WPF文本框在C#code设置背景​​颜色文本框、颜色、背景、WPF

2023-09-02 21:10:27 作者:柒.囍

我怎样才能改变一个WPF文本框的背景色和前景色编程在C#?

解决方案

  textBox1.Background = Brushes.Blue;
textBox1.Foreground = Brushes.Yellow;
 

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

  //使用System.Windows.Media;

textBox1.Background = Brushes.White;
textBox1.Background =新的SolidColorBrush(Color.White);
textBox1.Background =新的SolidColorBrush(Color.FromArgb(0xFF时,为0xFF,0,0);
textBox1.Background = System.Windows.SystemColors.MenuHighlightBrush;
 

How can I change the background and foreground colors of a WPF Textbox programatically in c#?

解决方案

textBox1.Background = Brushes.Blue;
textBox1.Foreground = Brushes.Yellow;

WPF Foreground and Background is of type System.Windows.Media.Brush. You can set another color like this:

// using System.Windows.Media;

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