在 UWP 中鼠标悬停时如何更改按钮的背景? [英] How to change the button's background when mouseover in UWP?
问题描述
现在我只想在鼠标悬停时将按钮的背景更改为#f5f5f5.
WPF具有触发功能,可以轻松完成.
而我听说UWP不再有触发器,而是有其他功能,就像这个问题:
UWP 项目不支持触发器元素 (XAML)
Now I only want to change the button's background to #f5f5f5 when mouseover it.
WPF has a trigger function which can do it easily.
Whereas I heard that UWP has no trigger any more,but with other function instead,like this question:
Trigger element (XAML) is not supported in a UWP project
我使用 DataTriggerBehavior 作为教程做到了:
I did it with DataTriggerBehavior as the tutorial:
<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,但我找不到教程,所以我不知道如何使用它.
更何况,我几乎不知道为什么微软不再使用触发器.
你能帮我解决我的问题吗?
非常感谢!
The programme can works successfully,but can't change the background.
Oh,my god.
I have tried the VisualState also,but I can't find a tutorial so I don't know how to use it.
What's more,I can hardly know why microsoft do not use trigger any more.
Would you please help me how to solve my problem?
Thanks a lot!
推荐答案
您可以使用 XAML Behavior SDK 但我会为您的 Button
使用自定义样式.您只需在 默认样式.
You can use triggers or behaviors from the XAML Behavior SDK but I'd go with a custom style for your Button
instead. You simply change its color in the PointerOver
state inside the default style.
<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>
或者,您可以在 App.xaml
中覆盖 ButtonBackgroundPointerOver
.
Alternatively, you can override ButtonBackgroundPointerOver
inside your App.xaml
.
<Application.Resources>
<SolidColorBrush x:Key="ButtonBackgroundPointerOver">#f5f5f5</SolidColorBrush>
</Application.Resources>
这篇关于在 UWP 中鼠标悬停时如何更改按钮的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!