如何覆盖样式中的资源?

时间:2015-03-13 08:42:13

标签: wpf resources

如何覆盖样式中的资源。在下面的示例中(借用另一个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>

1 个答案:

答案 0 :(得分:0)

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