【问题标题】:Set background color of WPF Textbox in C# code在 C# 代码中设置 WPF 文本框的背景颜色
【发布时间】:2010-11-02 01:36:15
【问题描述】:

如何在 C# 中以编程方式更改 WPF 文本框的背景和前景色?

【问题讨论】:

    标签: c# .net wpf background-color


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

    WPF 前景和背景的类型为 System.Windows.Media.Brush。您可以像这样设置另一种颜色:

    using System.Windows.Media;
    
    textBox1.Background = Brushes.White;
    textBox1.Background = new SolidColorBrush(Colors.White);
    textBox1.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0, 0));
    textBox1.Background = System.Windows.SystemColors.MenuHighlightBrush;
    

    【讨论】:

    • 如果我们想给颜色属性设置一个十六进制值,怎么做??
    • 你可以使用 Brush Brush = new SolidColorBrush(Color.FromRgb(r, g, b));
    • 还有更漂亮的LinearGradientBrush :)
    • 确保包含 System.Windows.Media。
    【解决方案2】:

    我认为您是在 XAML 中创建 TextBox?

    在这种情况下,您需要为文本框命名。然后在代码隐藏中,您可以使用各种画笔设置 Background 属性。其中最简单的是 SolidColorBrush:

    myTextBox.Background = new SolidColorBrush(Colors.White);
    

    【讨论】:

      【解决方案3】:

      你看过Color.FromRgb吗?

      【讨论】:

        【解决方案4】:

        如果您想使用十六进制颜色设置背景,您可以这样做:

        var bc = new BrushConverter();
        
        myTextBox.Background = (Brush)bc.ConvertFrom("#FFXXXXXX");
        

        或者您可以在 XAML 中设置 SolidColorBrush 资源,然后在代码隐藏中使用 findResource:

        <SolidColorBrush x:Key="BrushFFXXXXXX">#FF8D8A8A</SolidColorBrush>
        myTextBox.Background = (Brush)Application.Current.MainWindow.FindResource("BrushFFXXXXXX");
        

        【讨论】:

        • 最好使用(System.Windows.Media.Brush)Application.Current.FindResource("BrushFFXXXXX");,因为如果将来升级为使用多个调度程序线程,您的应用程序将不会引发线程异常。
        • 应该在哪里声明&lt;SolidColorBrush x:Key="BrushFFXXXXXX"&gt;#FF8D8A8A&lt;/SolidColorBrush&gt;?当我在&lt;Window x:Class ... 内尝试这样做时,出现错误:“属性‘内容’被设置了不止一次”
        【解决方案5】:

        您可以将十六进制转换为 RGB:

        string ccode = "#00FFFF00";
        int argb = Int32.Parse(ccode.Replace("#", ""), NumberStyles.HexNumber);
        Color clr = Color.FromArgb(argb);
        

        【讨论】:

        • System.Windows.Media.Color FromArgb 正在接受字节 a、字节 r、字节 g、字节 b,而不是 int
        【解决方案6】:

        您可以使用十六进制颜色:

        your_contorl.Color = DirectCast(ColorConverter.ConvertFromString("#D8E0A627"), Color)
        

        【讨论】:

          【解决方案7】:

          我知道这已在另一篇 SOF 帖子中得到解答。但是,如果您知道十六进制,则可以这样做。

          textBox1.Background = (SolidColorBrush)new BrushConverter().ConvertFromString("#082049");
          

          【讨论】:

            【解决方案8】:

            BrushConverter bc = new BrushConverter();

            textName.Background = (Brush)bc.ConvertFrom("#FF7BFF64");

            buttonName.Foreground = new SolidColorBrush(Colors.Gray);

            参考这里 https://www.it-mure.jp.net/ja/c%23/c%EF%BC%83%E3%82%B3%E3%83%BC%E3%83%89%E3%81%A7wpf%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%E3%83%9C%E3%83%83%E3%82%AF%E3%82%B9%E3%81%AE%E8%83%8C%E6%99%AF%E8%89%B2%E3%82%92%E8%A8%AD%E5%AE%9A%E3%81%99%E3%82%8B/957683208/

            【讨论】:

              猜你喜欢
              • 2016-03-14
              • 2013-07-29
              • 1970-01-01
              • 1970-01-01
              • 2013-01-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-04-14
              相关资源
              最近更新 更多