如何在鼠标悬停在UWP时更改按钮的背景?
问题描述:
现在我只想在鼠标悬停时将按钮的背景更改为#f5f5f5。
WPF具有触发功能,可以轻松完成。
虽然我听说UWP没有触发任何更多,但与其他函数来代替,像这样的问题:
Trigger element (XAML) is not supported in a UWP project如何在鼠标悬停在UWP时更改按钮的背景?
我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,但是我找不到教程,所以我不知道如何使用它。
更重要的是,我几乎不知道为什么微软不再使用触发器。
你能帮我解决我的问题吗?
非常感谢!
答
您可以使用触发器或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
内。
感谢您再次帮助我。我想我必须马上学习更多关于VisualState的知识。 –