我在XAML中有以下基础Style
,我需要根据它创建一些新样式,每种新样式只有1个不同的属性。
<UserControl.Resources>
<Style x:Key="BaseButtonStyle" TargetType="{x:Type ButtonBase}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Background" Value="{StaticResource Button.Background}"/>
<Setter Property="BorderBrush" Value="{StaticResource Button.BorderBrush}"/>
<Setter Property="Foreground" Value="{StaticResource Button.Foreground}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="10,1,10,1"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Grid x:Name="Root" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<Grid x:Name="FocusState" Opacity="0">
<Border CornerRadius="4" Style="{StaticResource FocusTemplate}"/>
</Grid>
<Grid x:Name="DefaultState">
<Border Background="{StaticResource Button.DefaultState.Border1}"
Padding="1" CornerRadius="3">
<Border Background="{StaticResource Button.DefaultState.Border2}"
Padding="1" CornerRadius="2">
</Border>
</Border>
</Grid>
<Grid x:Name="HoverState" Opacity="0">
<Border Background="{StaticResource Button.HoverState.Border1}"
Padding="1" CornerRadius="3">
<Grid>
<Border Background="{StaticResource Button.HoverState.Border2}"
Padding="1" CornerRadius="2">
<Border Background="{StaticResource Button.HoverState.Border3}"
CornerRadius="1"/>
</Border>
<Border>
<Border.Background>
<RadialGradientBrush>
<RadialGradientBrush.RelativeTransform>
<TranslateTransform X="0" Y="0.5"/>
</RadialGradientBrush.RelativeTransform>
<GradientStop Color="#00FFFFFF" Offset="1"/>
<GradientStop Color="#BFFFFFFF" Offset="0"/>
</RadialGradientBrush>
</Border.Background>
</Border>
</Grid>
</Border>
</Grid>
<Grid x:Name="PressedState" Opacity="0">
<Border Background="{StaticResource Button.PressedState.Border1}"
Padding="1" CornerRadius="3">
<Border Background="{StaticResource Button.PressedState.Border2}"
Padding="1" CornerRadius="2">
<Border Background="{StaticResource Button.PressedState.Border3}"
CornerRadius="1"/>
</Border>
</Border>
</Grid>
<Grid x:Name="CheckedState" Opacity="0">
<Border Background="{StaticResource Button.CheckedState.Border1}"
Padding="1" CornerRadius="3">
<Border Background="{StaticResource Button.CheckedState.Border2}"
Padding="1" CornerRadius="2">
<Border Background="{StaticResource Button.CheckedState.Border3}"
CornerRadius="1"/>
</Border>
</Border>
</Grid>
<Grid Margin="2">
<ContentPresenter Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Grid>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="DefaultState" Property="Opacity" Value="0"/>
<Setter TargetName="HoverState" Property="Opacity" Value="1"/>
<Setter TargetName="PressedState" Property="Opacity" Value="0"/>
<Setter TargetName="CheckedState" Property="Opacity" Value="0"/>
<Setter Property="Foreground"
Value="{StaticResource Button.Foreground}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="DefaultState" Property="Opacity" Value="0"/>
<Setter TargetName="HoverState" Property="Opacity" Value="0"/>
<Setter TargetName="PressedState" Property="Opacity" Value="1"/>
<Setter TargetName="CheckedState" Property="Opacity" Value="0"/>
<Setter Property="Foreground"
Value="{StaticResource Button.Foreground}"/>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="True">
<Setter TargetName="DefaultState" Property="Opacity" Value="0"/>
<Setter TargetName="HoverState" Property="Opacity" Value="0"/>
<Setter TargetName="PressedState" Property="Opacity" Value="0"/>
<Setter TargetName="CheckedState" Property="Opacity" Value="1"/>
<Setter Property="Foreground"
Value="{StaticResource Button.Foreground}"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="FocusState" Property="Opacity" Value="1"/>
</Trigger>
<Trigger Property="Button.IsDefaulted" Value="True">
<Setter TargetName="FocusState" Property="Opacity" Value="1"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Root" Property="Opacity" Value="0.35"/>
<Setter Property="Foreground"
Value="{StaticResource Button.Foreground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<UserControl.Resources>
我使用
将其设置为基本按钮样式<Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseButtonStyle}"/>
到目前为止一切顺利。接下来我想要第二种风格,唯一不同的部分是
<Border Background="{StaticResource Button.DefaultState.BorderNEWZ}"
//instead of
<Border Background="{StaticResource Button.DefaultState.Border2}"
所以我尝试过NEWSTYLETEST ......
<Style x:Key="NEWSTYLETEST" TargetType="{x:Type Button}" BasedOn="{StaticResource BaseButtonStyle}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Grid x:Name="Root" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<Grid x:Name="FocusState" Opacity="0">
<Border CornerRadius="4" Style="{StaticResource FocusTemplate}"/>
</Grid>
<Grid x:Name="DefaultState">
<Border Background="{StaticResource Button.DefaultState.Border1}"
Padding="1" CornerRadius="3">
<Border Background="{StaticResource Button.DefaultState.BorderNEWZ}"
Padding="1" CornerRadius="2">
</Border>
</Border>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这确实将我需要的控制部分从Button.DefaultState.Border2
更改为Button.DefaultState.BorderNEWZ
,但它省略了基本样式中其余<Setter Property="Template">
中包含的其余值。
我每次都可以复制整个<Setter Property="Template">
并更改我需要的行,但这似乎有些过分。有没有办法只针对每个新样式的变化定位Button.DefaultState.Border2
,同时保持其他所有内容不变,而不重复大块的代码?
答案 0 :(得分:1)
BasedOn基于每个属性工作,因此在这种情况下,您已经告诉它覆盖&#34;模板&#34;属性。 (全部或全部)最终,您将要找到的答案如下所示:
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/abe21d46-8e32-4f33-bb8f-750bdc38cb14/