Consider the following XAML code:
<StackPanel>
<ListBox x:Name="lbColor">
<ListBoxItem Content="Blue"/>
<ListBoxItem Content="Green"/>
<ListBoxItem Content="Yellow"/>
</ListBox>
<TextBlock>
<TextBlock.Text>
<Binding ElementName="lbColor" Path="SelectedItem.Content"/>
</TextBlock.Text>
<TextBlock.Background>
<Binding ElementName="lbColor" Path="SelectedItem.Content"/>
</TextBlock.Background>
</TextBlock>
</StackPanel>
I understand how Text property binding works. Internally it is converted to something like:
textBlock.Text = lbColor.SelectedItem.Content;
But how Background is bound to the same source? This code:
textBlock.Background = lbColor.SelectedItem.Content;
is incorrect. How can it work? BTW, it works and shows correct background color.
The only way I see, is to get System.Windows.Media.Colors property with given name, create SolidColorBrush from it and assign to Background property. But there is nothing in the code which points to this path.
This works because there is a built in conversion that allows WPF to convert from a
Stringto aBrush(which is the required type of theBackgroundproperty).If you look at the MSDN documentation for
Brush, you can see that it is decorated with aTypeConverterattribute that specifies a converter of typeBrushConverter.For general information about type converters, have a read of this article