如何在UWP中鼠标悬停时更改按钮的背景?

Mel*_* NG 2 uwp uwp-xaml

现在我只想在鼠标悬停时将按钮的背景更改为#f5f5f5.
WPF具有触发功能,可以轻松完成.
虽然我听说UWP不再有触发器,但是使用其他功能,就像这个问题:
UWP项目不支持触发元素(XAML)

我使用DataTriggerBehavior作为教程:

<ControlTemplate TargetType="Button" x:Key="ButtonControlTemplate">
            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" VerticalAlignment="{TemplateBinding VerticalAlignment}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Name="ButtonBorder" Background="{TemplateBinding Background}">                
                <ContentPresenter FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Foreground="{TemplateBinding Foreground}"></ContentPresenter>
            </Border>
            <Interactivity:Interaction.Behaviors>
                <Core:DataTriggerBehavior Binding="{Binding PointerMovedEvent, ElementName=ButtonBorder}" Value="true">
                    <Core:ChangePropertyAction TargetObject="{Binding ElementName=ButtonBorder}" PropertyName="Background" Value="#f5f5f5" />
                </Core:DataTriggerBehavior>
            </Interactivity:Interaction.Behaviors>
        </ControlTemplate>
Run Code Online (Sandbox Code Playgroud)


该程序可以成功运行,但不能改变背景.
哦,我的上帝.
我也尝试过VisualState,但我找不到教程,所以我不知道如何使用它.
更重要的是,我几乎不知道为什么微软不再使用触发器了.
你能帮我解决一下我的问题吗?
非常感谢!

Jus*_* XL 6

您可以使用XAML Behavior SDK中的触发器或行为,但我会使用自定义样式Button.您只需PointerOver默认样式内的状态更改其颜色.

<VisualState x:Name="PointerOver">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
            <DiscreteObjectKeyFrame KeyTime="0" Value="#f5f5f5"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPointerOver}"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPointerOver}"/>
        </ObjectAnimationUsingKeyFrames>
        <PointerUpThemeAnimation Storyboard.TargetName="RootGrid"/>
    </Storyboard>
</VisualState>
Run Code Online (Sandbox Code Playgroud)

或者,你可以ButtonBackgroundPointerOver在你的内部覆盖App.xaml.

<Application.Resources>
    <SolidColorBrush x:Key="ButtonBackgroundPointerOver">#f5f5f5</SolidColorBrush>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)