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

时间:2017-08-08 08:05:36

标签: uwp uwp-xaml

现在我只想将按钮的背景更改为#f5f5f5鼠标悬停时 WPF具有触发功能,可以轻松完成 虽然我听说UWP不再有触发器,但是还有其他功能,比如这个问题:
Trigger element (XAML) is not supported in a UWP project

我使用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>


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

2 个答案:

答案 0 :(得分:6)

您可以使用XAML Behavior SDK中的触发器或行为,但我会为您的Button使用自定义样式。您只需在default style内的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>

或者,您可以覆盖ButtonBackgroundPointerOver内的App.xaml

<Application.Resources>
    <SolidColorBrush x:Key="ButtonBackgroundPointerOver">#f5f5f5</SolidColorBrush>
</Application.Resources>

答案 1 :(得分:0)

更改页面上按钮的悬停颜色。

  <Button Background="#FF2C4763"   Click="HamburgerButton_Click" VerticalAlignment="Top" x:Name="HamburgerButton" FontFamily="Segoe MDL2 Assets" Content="&#xE700;">
     <Button.Resources>
        <ResourceDictionary>
            <ResourceDictionary.ThemeDictionaries>
                <ResourceDictionary x:Key="Dark">
                    <SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="Black"/>
                </ResourceDictionary>
            </ResourceDictionary.ThemeDictionaries>
          </ResourceDictionary>
        </Button.Resources>
    </Button>