Silverlight 4 - How can I change button background color when focused with an Implicit button style?

三世轮回 提交于 2019-12-22 08:44:22

问题


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?

Thanks in advance.

<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>
        ...

回答1:


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>



回答2:


Some good answers here:

Style the MouseOver on a Silverlight/WPF button http://forums.silverlight.net/forums/p/186402/427878.aspx



来源:https://stackoverflow.com/questions/4064363/silverlight-4-how-can-i-change-button-background-color-when-focused-with-an-im

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!