我如何能改变一个WPF文本框的背景和前景的颜色在c#编程?


当前回答

你看过Color.FromRgb吗?

其他回答

BrushConverter bc = new BrushConverter();

textName。背景=(bruce)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/

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

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

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

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");

你看过Color.FromRgb吗?

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

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