[英]How to change HyperlinkButton background and foreground color when PointerEntered in UWP
这是我在C#和XAML中的示例UWP代码,在其中我在MainPage方法中创建了一个超链接按钮,并设置了诸如前景和背景之类的样式属性。
现在我想在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
的Background和Foreground颜色,该颜色将覆盖控件的“正常”可视状态的颜色。当指针位于控件上方时,XAML中预定义的默认控件样式( 用于“ PointerOver”状态 )正在显示其应具有的颜色,因此,只有从控件中删除指针后,您才能看到更改。
因此,在我看来,覆盖控件的xaml样式是PointerOver事件的正确和最佳解决方案。
请询问有关此问题的其他任何问题。如果需要,我很乐意提供适当的解释。
您应该这样做-
hLinkButton.PointerEntered += hLinkButton_OnPointerEntered;
private void hLinkButton_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;
}
此外,如果您想将所有内容都撤消,则应使用Pointer hLinkButton_PointerExited事件
更新资料
什么是实际问题,您的代码看起来一切正常。
与您的代码一起输出
此外 ,一旦完成,您就可以声明您的超级链接按钮
public sealed partial class MainPage : Page
{
HyperlinkButton hLinkButton = new HyperlinkButton();
public MainPage()
{
this.InitializeComponent();
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)
{
hLinkButton.Background = new SolidColorBrush(Colors.White);
hLinkButton.Foreground = new SolidColorBrush(Colors.Red);
hLinkButton.FontSize = 30.0;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.