Définir la couleur de fond de WPF dans la zone de texte du code C #
-
13-09-2019 - |
Question
Comment puis-je changer l'arrière-plan et les couleurs de premier plan d'un programme dans WPF Textbox C #?
La solution
textBox1.Background = Brushes.Blue;
textBox1.Foreground = Brushes.Yellow;
Premier plan WPF et arrière-plan est de type System.Windows.Media.Brush
. Vous pouvez définir une autre couleur comme ceci:
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;
Autres conseils
Si vous souhaitez définir l'arrière-plan en utilisant une couleur hexadécimal, vous pouvez faire ceci:
var bc = new BrushConverter();
myTextBox.Background = (Brush)bc.ConvertFrom("#FFXXXXXX");
Ou vous pouvez configurer une ressource SolidColorBrush en XAML, puis utilisez FindResource dans le code-behind:
<SolidColorBrush x:Key="BrushFFXXXXXX">#FF8D8A8A</SolidColorBrush>
myTextBox.Background = (Brush)Application.Current.MainWindow.FindResource("BrushFFXXXXXX");
Je suppose que vous créez la zone de texte en XAML?
Dans ce cas, vous devez donner à la zone de texte un nom. Ensuite, dans le code-behind vous pouvez définir la propriété en arrière-plan en utilisant une variété de brosses. Le plus simple qui est le SolidColorBrush:
myTextBox.Background = new SolidColorBrush(Colors.White);
Vous pouvez utiliser des couleurs hexagonales:
your_contorl.Color = DirectCast(ColorConverter.ConvertFromString("#D8E0A627"), Color)
Vous pouvez convertir en RGB hexadécimal:
string ccode = "#00FFFF00";
int argb = Int32.Parse(ccode.Replace("#", ""), NumberStyles.HexNumber);
Color clr = Color.FromArgb(argb);
Avez-vous pris un coup d'œil à Color.FromRgb
?