我想Button
使用重新定义的模板和动画创建一个通用样式,以在鼠标进入、退出、向上、向下以及禁用和启用状态之间进行转换。这不是问题,但我想做另一个按钮样式,除了背景颜色外,基本相同。
我在样式资源和情节提要中为Normal、Hover和Disabled状态定义了颜色:
<Style.Resources>
<Color x:Key="DisabledBackground">#4c4c4c</Color>
<Color x:Key="NormalBackground">#538ce1</Color>
<Color x:Key="HoverBackground">#6ea8ff</Color>
<Storyboard x:Key="MouseOverAnimation">
<ColorAnimation Storyboard.TargetName="BackgroundBrush"
Storyboard.TargetProperty="Color"
To="{StaticResource HoverBackground}"
Duration="0:0:0.3" />
<DoubleAnimation Storyboard.TargetName="Underlay"
Storyboard.TargetProperty="Opacity"
To="0.7"
Duration="0:0:0.3" />
</Storyboard>
<!-- and few others... -->
</Style>
然后我定制了模板,最后是这个ControlTemplate.Triggers
部分:
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{DynamicResource MouseOverAnimation}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{DynamicResource MouseOutAnimation}"/>
</Trigger.ExitActions>
</Trigger>
<!-- and few others... -->
现在我想要的是创建新样式并更改颜色,DisabledBackground
如下NormalBackground
所示:
<Style x:Key="Start"
TargetType="{x:Type Button}"
BasedOn="{StaticResource {x:Type Button}}">
<Style.Resources>
<Color x:Key="DisabledBackground">#4c4c4c</Color>
<Color x:Key="NormalBackground">#960a0a</Color>
<Color x:Key="HoverBackground">#de1111</Color>
</Style.Resources>
</Style>
并让控件模板原封不动。您可能已经注意到,我DynamicResource
在常用按钮样式中使用了以异常结尾的样式资源中的情节提要,因为情节提要不能具有绑定或动态资源。这是我最后一个不起作用的“解决方案”,但我想不出别的办法。
我不想复制和粘贴我的整个按钮样式只是为了更改两种颜色。如何修改我的样式,以便能够“动态”更改故事板动画中使用的颜色,或者至少继承样式并在那里设置颜色?
完成 XAML
<Style TargetType="{x:Type Button}">
<Style.Resources>
<Color x:Key="DisabledBackground">#4c4c4c</Color>
<Color x:Key="NormalBackground">#538ce1</Color>
<Color x:Key="HoverBackground">#6ea8ff</Color>
<Storyboard x:Key="MouseOverAnimation">
<ColorAnimation Storyboard.TargetName="BackgroundBrush" Storyboard.TargetProperty="Color" To="{StaticResource HoverBackground}" Duration="0:0:0.3" />
<DoubleAnimation Storyboard.TargetName="Underlay" Storyboard.TargetProperty="Opacity" To="0.7" Duration="0:0:0.3" />
</Storyboard>
<Storyboard x:Key="MouseOutAnimation" FillBehavior="Stop">
<ColorAnimation Storyboard.TargetName="BackgroundBrush" Storyboard.TargetProperty="Color" To="{StaticResource NormalBackground}" Duration="0:0:0.3" />
<DoubleAnimation Storyboard.TargetName="Underlay" Storyboard.TargetProperty="Opacity" To="0.2" Duration="0:0:0.3" />
</Storyboard>
<Storyboard x:Key="MouseDownAnimation">
<DoubleAnimation Storyboard.TargetName="OverlayGradient" Storyboard.TargetProperty="Opacity" To="0.45" Duration="0:0:0.1" />
</Storyboard>
<Storyboard x:Key="MouseUpAnimation" Storyboard.TargetProperty="Background" FillBehavior="Stop">
<DoubleAnimation Storyboard.TargetName="OverlayGradient" Storyboard.TargetProperty="Opacity" To="0.5" Duration="0:0:0.1" />
</Storyboard>
<Storyboard x:Key="DisabledAnimation">
<ColorAnimation Storyboard.TargetName="BackgroundBrush" Storyboard.TargetProperty="Color" To="{StaticResource DisabledBackground}" Duration="0:0:0.3" />
<ColorAnimation Storyboard.TargetName="UnderlayFillBrush" Storyboard.TargetProperty="Color" To="{StaticResource DisabledBackground}" Duration="0:0:0.3" />
</Storyboard>
<Storyboard x:Key="EnabledAnimation">
<ColorAnimation Storyboard.TargetName="BackgroundBrush" Storyboard.TargetProperty="Color" To="{StaticResource NormalBackground}" Duration="0:0:0.3" />
<ColorAnimation Storyboard.TargetName="UnderlayFillBrush" Storyboard.TargetProperty="Color" To="{StaticResource NormalBackground}" Duration="0:0:0.3" />
</Storyboard>
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<!-- Button underlay glow
-->
<Rectangle x:Name="Underlay" Opacity="0.2">
<Rectangle.Fill>
<SolidColorBrush x:Name="UnderlayFillBrush" Color="{DynamicResource NormalBackground}"/>
</Rectangle.Fill>
<Rectangle.Effect>
<BlurEffect Radius="35" KernelType="Gaussian"/>
</Rectangle.Effect>
</Rectangle>
<!-- Button base border with rounded corners
Contains base background
-->
<Border x:Name="ButtonBackground" BorderThickness="1" CornerRadius="2">
<Border.BorderBrush>
<SolidColorBrush Color="Black" Opacity="0.8"/>
</Border.BorderBrush>
<Border.Background>
<SolidColorBrush x:Name="BackgroundBrush" Color="{DynamicResource NormalBackground}"/>
</Border.Background>
<!-- Button Overlay
Adds the background overlay gradient -->
<Border CornerRadius="2">
<Border.Background>
<LinearGradientBrush x:Name="OverlayGradient" Opacity="0.5" StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="White"/>
<GradientStop Offset="0.02" Color="White"/>
<GradientStop Offset="0.02" Color="Transparent"/>
<GradientStop Offset="0.85" Color="#000000" />
</LinearGradientBrush>
</Border.Background>
<Border BorderThickness="1" CornerRadius="2">
<Border.BorderBrush>
<SolidColorBrush Color="#b4b4b4" Opacity="0.2"/>
</Border.BorderBrush>
<!-- Inner text -->
<TextBlock Text="{TemplateBinding Content}"
FontSize="{TemplateBinding FontSize}"
FontFamily="Segoe UI"
Foreground="White"
TextWrapping="Wrap"
HorizontalAlignment="Center"
VerticalAlignment="Center"
TextOptions.TextFormattingMode="Display"
RenderOptions.BitmapScalingMode="NearestNeighbor">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="0" BlurRadius="6" Color="Black" RenderingBias="Quality"/>
</TextBlock.Effect>
</TextBlock>
</Border>
</Border>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{DynamicResource DisabledAnimation}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{DynamicResource EnabledAnimation}"/>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{DynamicResource MouseOverAnimation}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{DynamicResource MouseOutAnimation}"/>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{DynamicResource MouseDownAnimation}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{DynamicResource MouseUpAnimation}"/>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>