【问题标题】:How to override a Resource within a Style ?如何覆盖样式中的资源?
【发布时间】:2015-03-13 08:42:13
【问题描述】:

如何覆盖样式中的资源。在下面的示例中(借用另一个 Stackoverflow-Answer),我定义了 2 种样式 - textButtonDarktextButtonLight,它们基于 textButtonDark。我想将textButtonDark 的前景色更改为红色,将textButtonDark 的前景色更改为蓝色,而不使用textButtonDark 的全新样式。我怎样才能做到这一点?下面的 DynamicResource 方法根本不起作用。

<Page
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

      <Page.Resources>
        <!--Control colors.-->
        <Color x:Key="ControlNormalColor">Transparent</Color>
        <Color x:Key="ControlMouseOverColor">#FFd2d2d2</Color>   
        <Color x:Key="DisabledControlColor">#FFF2F2F2</Color>
        <Color x:Key="DisabledForegroundColor">#FFBFBFBF</Color>
        <Color x:Key="ControlPressedColor">#FFd2d2d2</Color>

        <!-- FocusVisual -->
        <Style x:Key="ButtonFocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border>
                            <Rectangle Margin="2" StrokeThickness="1" Stroke="#60000000" StrokeDashArray="1 2" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!-- Button -->
        <Style TargetType="Button" x:Key="textButtonDark">
        <Style.Resources>
          <Color x:Key="ControlMouseOverForegroundColor">Red</Color>
        </Style.Resources>                  
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <Setter Property="OverridesDefaultStyle" Value="true" />
            <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}" />
            <Setter Property="Foreground" Value="#FF000000" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border TextBlock.Foreground="{TemplateBinding Foreground}" x:Name="Border" CornerRadius="5">
                            <Border.Background>
                                <SolidColorBrush  Color="{DynamicResource ControlNormalColor}" />
                            </Border.Background>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0:0:0.1" />
                                        <VisualTransition GeneratedDuration="0" To="Pressed" />
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Normal" />
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Border">
                                                <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlMouseOverColor}" />
                                            </ColorAnimationUsingKeyFrames>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)">
                                                <EasingColorKeyFrame KeyTime="0" Value="{DynamicResource ControlMouseOverForegroundColor}" />
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
                                                Storyboard.TargetName="Border">
                                                <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlPressedColor}" />
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
                                                Storyboard.TargetName="Border">
                                                <EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledControlColor}" />
                                            </ColorAnimationUsingKeyFrames>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                                Storyboard.TargetName="Border">
                                                <EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledForegroundColor}" />
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <ContentPresenter Margin="8,2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

      <Style TargetType="Button" x:Key="textButtonLight" BasedOn="     {StaticResource textButtonDark}">
        <Style.Resources>
          <Color x:Key="ControlMouseOverForegroundColor">Blue</Color>
        </Style.Resources>    
        <Setter Property="Foreground" Value="#404040" />
      </Style>

    </Page.Resources>


  <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
    <StackPanel Orientation="Horizontal">
      <Button Style="{StaticResource textButtonDark}">DarkButton</Button>      
      <Button Style="{StaticResource textButtonLight}">LightButton</Button>
    </StackPanel>
  </Grid>
</Page>

【问题讨论】:

    标签: wpf resources


    【解决方案1】:

    您可以在单个控件上设置前景(或任何其他属性),它将覆盖样式中定义的值。

    【讨论】:

    • 是的,我知道——但就我而言,前景不是我想要的。我想设置在 ControlTemplate 中使用的 ControlMouseOverForegroundColor,以便在鼠标悬停在控件上时为前景色设置动画
    • 啊,好吧,这是一个更棘手的案例。老实说,我可能会选择最简单的解决方案并定义不同的样式,然后根据布尔标志或枚举参数使用转换器选择我想要的样式。
    猜你喜欢
    • 1970-01-01
    • 2020-05-19
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多