如何在 UWP 中设置 RatingControl 星星的颜色?

Har*_* Es 2 xaml uwp

RatingControl在我的 UWP 应用程序中添加了一个。如何设置填充星星和空星星的颜色?这是我添加的代码:

<RatingControl x:Name="MyRating" Value="3.5" Width="300" Height="200"  />
Run Code Online (Sandbox Code Playgroud)

Mar*_*und 5

如果您检查 的默认样式RatingControl,您可以找到以下内容VisualStateGroup

<VisualStateGroup x:Name="CommonStates">
    <VisualState x:Name="Disabled">
        <VisualState.Setters>
            <Setter Target="ForegroundContentPresenter.Foreground" 
              Value="{ThemeResource RatingControlDisabledSelectedForeground}"/>
        </VisualState.Setters>
    </VisualState>
    <VisualState x:Name="Placeholder">
        <VisualState.Setters>
            <Setter Target="ForegroundContentPresenter.Foreground"
               Value="{ThemeResource RatingControlPlaceholderForeground}"/>
        </VisualState.Setters>
    </VisualState>
    <VisualState x:Name="PointerOverPlaceholder">
        <VisualState.Setters>
            <Setter Target="ForegroundContentPresenter.Foreground" 
               Value="{ThemeResource RatingControlPointerOverPlaceholderForeground}"/>
        </VisualState.Setters>
    </VisualState>
    <VisualState x:Name="PointerOverUnselected">
        <VisualState.Setters>
            <Setter Target="ForegroundContentPresenter.Foreground" 
                Value="{ThemeResource RatingControlPointerOverUnselectedForeground}"/>
        </VisualState.Setters>
    </VisualState>
    <VisualState x:Name="Set">
        <VisualState.Setters>
            <Setter Target="ForegroundContentPresenter.Foreground"
                Value="{ThemeResource RatingControlSelectedForeground}"/>
        </VisualState.Setters>
    </VisualState>
    <VisualState x:Name="PointerOverSet">
        <VisualState.Setters>
            <Setter Target="ForegroundContentPresenter.Foreground" 
                Value="{ThemeResource RatingControlSelectedForeground}"/>
        </VisualState.Setters>
    </VisualState>
</VisualStateGroup>
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,颜色是基于资源的喜欢RatingControlSelectedForegroundRatingControlPointerOverUnselectedForeground等等。

您可以将这些自定义覆盖作为单独的资源提供,也可以编辑控件的样式。

注意 - 空星星颜色

虽然它不是模板的一部分,但您可以通过修改RatingControlUnselectedForeground资源来自定义空星颜色。

资源覆盖

您可以在单个评级控件级别、任何父级或应用程序级别覆盖资源。

覆盖RatingControl级别将仅适用于此单个RatingControl

<RatingControl x:Name="MyRating" Value="3.5" Width="300" Height="200" >
    <RatingControl.Resources>
       <SolidColorBrush x:Key="RatingControlSelectedForeground" Color="Red" />
       <SolidColorBrush Color="Blue" x:Key="RatingControlUnselectedForeground" />
    </RatingControl.Resources>
</RatingControl>
Run Code Online (Sandbox Code Playgroud)

您可以在任何父级上进行覆盖,例如在页面上:

<Page.Resources>
    <SolidColorBrush x:Key="RatingControlSelectedForeground" Color="Red" />
    <SolidColorBrush Color="Blue" x:Key="RatingControlUnselectedForeground" />
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)

或者最后你可以把它放在应用程序级别,如果你将它添加<Application.Resources>App.xaml. 然后它将适用RatingControl于应用程序中的任何内容。

替代解决方案 - 自定义样式

如果您想要更多控制甚至更好的自定义,您可以直接编辑默认RatingControl样式及其模板。RatingControl在设计器(或文档大纲窗口)中右键单击并选择Edit style,然后Edit a copy,设置自定义名称和应该放置样式的位置,然后单击 OK 确认。这将创建控件默认模板的副本,您可以在其中编辑VisualState Setter值以匹配所需的配色方案。

另请注意,您仍必须为空星( RatingControlUnselectedForeground)提供自定义资源。