Silverlight 4 - 使用隐式按钮样式聚焦时如何更改按钮背景颜色? [英] Silverlight 4 - How can I change button background color when focused with an Implicit button style?
问题描述
我在尝试实现本应微不足道的事情时遇到了很多困难.我正在使用在全局 XAML 资源文件中定义的隐式按钮样式.我只想使用 ColorAnimation 将 焦点按钮 的背景颜色更改为红色.我在 Storyboard.TargetProperty
和 Storyboard.TargetName
中尝试了许多不同的组合,但没有任何效果.我怎样才能做到这一点?
I'm having a great deal of difficulty trying to achieve something that should be trivial. I'm using an Implicit Button Style defined in a global XAML resource file. I just want to change the background color of the focused button to red with a ColorAnimation. I've tried a number of different combinations in Storyboard.TargetProperty
and Storyboard.TargetName
and nothing has worked. How can I achieve this?
提前致谢.
<Style TargetType="Button" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused" >
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Button" From="Green" To="Red" Duration="00:00:01" />
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
...
推荐答案
因为我没有你的其余样式,所以我用两个边框和一个 ContentPresenter 制作了这个.一旦聚焦,这会将按钮的背景从绿色变为红色.
Since I don't have the rest of your Style I made this with two Borders and a ContentPresenter. This animates the Background of the Button from Green to Red once focused.
<Style TargetType="Button" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<Storyboard>
<ColorAnimation Storyboard.TargetName="border"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
From="Green"
To="Red"
Duration="0:0:1" />
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border BorderBrush="Transparent" BorderThickness="1" CornerRadius="4">
<Border x:Name="border" Background="White" BorderBrush="Black" BorderThickness="1" CornerRadius="4">
</Border>
</Border>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这篇关于Silverlight 4 - 使用隐式按钮样式聚焦时如何更改按钮背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!