C# WPF Multiple Bindings on an Object

In this C# WPF example, we offer the user several ways to change a single control on the window. That control is simply some text. Text has several properties such as font, bold, italic and colour. and the user makes their choices using more than one control – one control for each property. You have multiple sources and one target. The code for that example comes from the book Illustrated WPF.

From the opposite perspective, you may offer the user one control that, if selected, changes a wholw set of properties of an object. It would be like choosing a skin or colour theme for an application. The user chooses “Light Blue” from a list of choices and that affects several controls and windows in the application. This scenario is not discussed here.

For example, the window shown below contains a Label at the top and two ComboBoxes beneath it. The top ComboBox contains a list of font families and is bound to the FontFamily property of the Label. The second ComboBox contains a set of FontWeights and is bound to the FontWeight property of the Label. When a new selection is made in either of the ComboBoxes the text in the Label reflects the change.


<Window x:Class="MultipleBindingsOnAnElement.MainWindow"
        Title="MainWindow" Height="150" Width="225">
        <Label Name="displayText" Margin="5" FontSize="16" Content="My Text"
             FontFamily="{Binding ElementName=fontBox, Path=Text}"
             FontWeight="{Binding ElementName=weightBox, Path=Text}"/>
        <ComboBox Name="fontBox" SelectedIndex="0" Margin="5,0,5,2">
            <ComboBoxItem>Courier New</ComboBoxItem>
        <ComboBox Name="weightBox" SelectedIndex="0" Margin="5,0,5,2">

Here is a diagram from the book Illustrated WPF.


Leave a comment

Your email address will not be published. Required fields are marked *