#209 – Specifying a Color Where a Brush Is Expected in XAML
February 6, 2011 Leave a comment
We saw earlier how to set the Background property for a control in XAML, by setting the property to an instance of a SolidColorBrush and then setting the Color property for the brush:
<Window.Background> <SolidColorBrush Color="PowderBlue"/> </Window.Background>
We can be much more concise than this, however, just setting the value of the Background property directly to a color:
<Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="346" Width="590" Background="PowderBlue">
This works through the use of type converters. The Background property uses a type converter to convert a string to an instance of a SolidColorBrush of the corresponding color. Specifically, it sets the property to point to the preexisting Brushes.PowderBlue brush. You can see the full list of predefined brushes here.