Background property accepts Brush . Therefore, the code can set the color as follows:
MyLabel.Background = Brushes.Aquamarine;
Or that:
SolidColorBrush myBrush = new SolidColorBrush(Colors.Red); MyLabel.Background = myBrush;
To set any color, you can use BrushConverter :
BrushConverter MyBrush = new BrushConverter(); MyLabel.Background = (Brush)MyBrush.ConvertFrom("#ABABAB");
Setting a property in LinearGradientBrush in code:
LinearGradientBrush myBrush = new LinearGradientBrush(); myBrush.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0)); myBrush.GradientStops.Add(new GradientStop(Colors.Green, 0.5)); myBrush.GradientStops.Add(new GradientStop(Colors.Red, 1.0)); MyLabel.Background = myBrush;
It will look like this for you:
private void Window_ContentRendered(object sender, EventArgs e) { TreeViewItem childNode = new TreeViewItem() { Header = new StackPanel { Orientation = Orientation.Horizontal, Children = { new Border { Width = 12, Height = 14, Background = Brushes.Yellow,
source share