鼠标悬停在Windows Phone 8上时更改按钮背景颜色

时间:2015-06-04 08:56:08

标签: windows xaml

我在Windows 8应用程序中为我的按钮使用自定义样式,如下所示:

    <Style x:Key="ButtonDefaultStyle" TargetType="Button">
        <Setter Property="Foreground" Value="Red"/>
        <Setter Property="Background" Value="Green"/>
        <Setter Property="MinWidth" Value="0"/>
        <Setter Property="MinHeight" Value="0" />
    </Style>

当鼠标悬停在按钮上时,更改背景颜色的最佳方法是什么,而不为背景添加自定义图像?

1 个答案:

答案 0 :(得分:1)

这是我的解决方案,当鼠标悬停时改变背景颜色:

 <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="grid">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" >
                                </VisualState>
                                <VisualState x:Name="PointerOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush)" Storyboard.TargetName="Border">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Null}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderThickness)" Storyboard.TargetName="Border">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Thickness>0</Thickness>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource GreenAppleColorBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity"
                                    Storyboard.TargetName="FocusVisualWhite" />
                                        <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity"
                                    Storyboard.TargetName="FocusVisualBlack" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unfocused" />
                                <VisualState x:Name="PointerFocused" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="Border"
                    Background="{TemplateBinding Background}" Margin="3">
                            <ContentPresenter x:Name="ContentPresenter"
                        ContentTemplate="{TemplateBinding ContentTemplate}"
                        ContentTransitions="{TemplateBinding ContentTransitions}"
                        Content="{TemplateBinding Content}"
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        Margin="{TemplateBinding Padding}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                        </Border>
                        <Rectangle x:Name="FocusVisualWhite" IsHitTestVisible="False" Opacity="0"
                    StrokeDashOffset="1.5" StrokeEndLineCap="Square"
                    Stroke="{StaticResource FocusVisualWhiteStrokeThemeBrush}" StrokeDashArray="1,1" />
                        <Rectangle x:Name="FocusVisualBlack" IsHitTestVisible="False" Opacity="0"
                    StrokeDashOffset="0.5" StrokeEndLineCap="Square"
                    Stroke="{StaticResource FocusVisualBlackStrokeThemeBrush}" StrokeDashArray="1,1" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>