【问题标题】:How to get button's background color as hex in XAML codebehind?如何在后面的 XAML 代码中将按钮背景颜色设为十六进制?
【发布时间】:2017-02-03 13:14:35
【问题描述】:

我想从 xaml 代码隐藏中获取按钮的背景颜色,请帮助我

【问题讨论】:

  • 你的代码在哪里?见How to Ask
  • (yourButton.Background as SolidColordBrush).Color 为您提供颜色。然后,您可以根据需要(Color.R、Color.B、Color.G、Color.A)将其转换为六进制或其他任何形式。
  • @Kilazu 对不起,我是新手,我认为问题本身解释清楚,下次我会添加更多信息:)
  • @Kilazur 非常感谢你:)

标签: wpf xaml silverlight


【解决方案1】:

如果您在 XAML 标记中为 Button 指定 x:Name...:

<Button x:Name="btn" Background="Yellow" />

...您可以使用此名称在代码隐藏中访问它:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        SolidColorBrush brush = btn.Background as SolidColorBrush;
        if(brush != null)
        {
            Color color = brush.Color;
            if(color == Colors.Yellow)
            {
                //...
            }
        }
    }
}

【讨论】:

  • 非常感谢我找了这么久:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多