【问题标题】:Set color of a textbox programatically in a WPF application在 WPF 应用程序中以编程方式设置文本框的颜色
【发布时间】:2014-01-04 19:09:17
【问题描述】:

我尝试在我的 WPF 应用程序中设置文本框的颜色

为什么会出现这个错误?

以及如何纠正?

【问题讨论】:

    标签: c# wpf colors background textbox


    【解决方案1】:
    textBox1.Background = Brushes.Blue;
    textBox1.Foreground = Brushes.Yellow;
    

    你可以这样做。

    【讨论】:

      【解决方案2】:

      它在错误中正确地告诉你。 Background 属性的类型是 System.Windows.Media.Brush,而不是 System.Windows.Media.Color,因此您不能为其分配 Color

      Brush 而非Color 传递到您的方法中,并将其分配给Background 属性。

      public void addToStackPanel(string argBuiltAlarm, Brush brush)
      {
          ...
          TextBox textBox = new TextBox { Background = brush };
          ...
      

      【讨论】:

      • 或者,可以从给定的颜色创建画笔:Brush brush = new SolidColorBrush(color)
      • Solved.Thanks.and @elgonzo,你也完全正确(我使用了你的解决方案)
      猜你喜欢
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 2012-12-25
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多