【问题标题】:How to change HyperlinkButton background and foreground color when PointerEntered in UWP在UWP中输入Pointer时如何更改HyperlinkBut​​ton背景和前景色
【发布时间】:2018-04-30 22:14:38
【问题描述】:

这是我的 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

【问题讨论】:

    标签: c# uwp windows-10-universal uwp-xaml


    【解决方案1】:

    您可以覆盖 HyperlinkBut​​ton 的样式模板来实现此目的。

    为此,您需要右键单击 HyperLinkBut​​ton > 编辑模板 > 编辑副本。 然后将相关代码添加到视觉状态。在您的情况下,视觉状态是“PointerOver”。

    以下代码将使 HyperLinkBut​​ton 的前景为红色,背景为白色。

      <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 中预定义的默认控件样式(for "PointerOver" 状态)正在显示它应该显示的颜色,因此您只能在将指针从您的控制..

    因此,在我看来,覆盖控件的 xaml 样式是 正确的,也是 PointerOver 事件的最佳解决方案..

    如果您对此可能有任何进一步的问题。如果需要,我很乐意提供适当的解释。

    【讨论】:

    • 嗨@Pratyay,感谢您的回复,但根据我的要求,我不应该使用任何 XAML 代码,我只能使用 C# 代码。
    • @Mahesh 我已经编辑了我的答案,并添加了解释为什么您当前的代码不起作用以及为什么不建议从 C# 执行此类操作..
    • 感谢您的建议,使用 XAML 按预期工作,但我仍然只能用 C# 编写它。我现在正在学习 C#。如果您有,请分享 C# 示例代码/链接吗
    • 我想我们可以在 C# 中通过定义 VisualStateManager 并定义它的 VisualStateGroups 来做到这一点,但这会不必要地过于复杂。不幸的是,我从来没有尝试用后面的代码覆盖它们。如果你熟悉 Web 技术,想象一下,如果你用 Javascript 完成了 HTML 的所有样式。当然我们可以做到。但是我们有 CSS 是有原因的。 .另外,如果您找到产生相同效果的好的c#示例,请发布URL,我也想了解更多信息..!
    【解决方案2】:

    你应该这样做 -

    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;
    }
    

    如果你想把所有的事情都反转回来,你应该使用指针 hLinkBut​​ton_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;
        }
    }
    

    【讨论】:

    • 我也是这样做的。您只是更改方法名称有什么意义。请检查我的问题。
    • @Mahesh 它在两种方法中都在改变颜色和字体大小没有区别,实际问题是什么,您可以在超链接按钮中设置内容以进行检查
    • @Shuham Sahu,我更新了代码,1. 名称 2. 内容 3. 字体粗细 4. 字体大小。目标版本:Win 10 周年纪念版(10.0;Build 14393),Min Build 为 10586。
    • 您的屏幕截图图像也显示相同的结果,例如鼠标悬停在背景和前景色上并没有像我们预期的那样改变。那是显示默认颜色。正如我提到的,只有字体大小会改变。
    • 好的!你想让鼠标悬停在超链接按钮上的灰色也应该是红色的吗?这是一个真正的问题,因为它是 xaml 样式的超链接按钮的默认属性,您可以使用 blend 和 c# 等来编辑它。让我用我的代码测试它....
    猜你喜欢
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 2013-08-30
    相关资源
    最近更新 更多