如何在UWP中更改PointerEntered时的HyperlinkButton背景和前景颜色 [英] How to change HyperlinkButton background and foreground color when PointerEntered in UWP
本文介绍了如何在UWP中更改PointerEntered时的HyperlinkButton背景和前景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在我想在PointerEntered事件触发时更改背景和前景颜色,在这种情况下只更改字体大小,而不更改颜色
public MainPage(){
this.InitializeComponent();
var hLinkButton = new HyperlinkButton();
hLinkButton.Name = "LearnMoreHyperLink";
hLinkButton.Content = "Learn More...";
hLinkButton.Background = new SolidColorBrush(Colors.Red);
hLinkButton.Foreground = new SolidColorBrush(Colors.White);
hLinkButton.FontWeight = FontWeights.SemiBold;
hLinkButton.FontSize = 18.0;
hLinkButton.PointerEntered += OnPointerEntered;
LayoutGrid.Children.Add(hLinkButton);
}
private void OnPointerEntered(object sender, PointerRoutedEventArgs e){
var hLButton = sender as HyperlinkButton;
hLButton.Background = new SolidColorBrush(Colors.White);
hLButton.Foreground = new SolidColorBrush(Colors.Red);
hLButton.FontSize = 30.0;
}
// Only fontsize effecting on mouseover, background and foreground is not working
推荐答案
您可以重写HyperlinkButton的样式模板以实现此目的。
为此,您需要右键单击您的HyperLinkButton>编辑模板>编辑副本。 然后将相关代码添加到视觉状态。在您的例子中,视觉状态是"PointerOver
"。
下面的代码将使HyperLinkButton的前景变为红色,背景变为白色。
<VisualState x:Name="PointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="#FFFF0000" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="#FFFFFFFF" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
编辑
在C#代码中,您所做的是更改HyperLinkButton
的背景和前景色,这将覆盖控件的"正常"视觉状态的颜色。当指针位于控件上方时,XAML中预定义的默认控件样式(表示"PointerOver"状态)显示的是它应该显示的颜色,因此您只能在从控件中移除指针后才能看到更改。
因此,在我看来,重写控件的XAML样式是正确的,也是PointerOver事件的最佳解决方案。
请询问您对此可能有任何进一步的问题。如果需要,我很乐意给出适当的解释。这篇关于如何在UWP中更改PointerEntered时的HyperlinkButton背景和前景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文