Background
property accepts an Brush
. Therefore, the code can set the color as follows:
MyLabel.Background = Brushes.Aquamarine;
Or this:
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 the property to the 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;
For you it would look like this:
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, // Set background here
},
new Label
{
Content = "Child1",
Background = Brushes.Pink, // Set background here
}
}
}
};
MyTreeView.Items.Add(childNode);
}