Binding multiple properties with XAML/C#

If you are programming with Visual Studio and creating WPF projects, you will often use Bindings within you xaml-files to a property e.g. within viewmodel. Bindings are very practically but sometime also frustrating and cumbersome if you have a special use case or some run time errors.

One special case is if you like to bind several properties to one element. For that case you can use Multibindings.

In the following example we show you how to bind multiple properties to one element or element properties.

Example

In that example a textbox should be enabled depending from the status of to checkboxes and depending from a GridView. The textbox should be enabled if the GridView has at least on item or if one of the both checkboxes is checked. In that example a viewmodel (MVVM) is used.

Viewmodel – Properties

Each of the both checkboxes from our example are bound to a bool value. And the GridView items are binded to an ObservableCollection.

public ObservableCollection<MyObject> MyGridViewItems
{
    get => _mygridviewitems;
    set => Set(() => MyGridViewItems, ref _mygridviewitems, value);
}
public bool MyCheckbox1
{
    get => _mycheckbox1;
    set => Set(() => CheckWeekMo, ref _checkWeekMo, value);
}
public bool MyCheckbox2
{
    get => _mycheckbox2;
    set => Set(() => CheckWeekTue, ref _checkWeekTue, value);
}

XAML-file: use MultiBinding

That means for our example we must bind to the property “IsEnabled” of the textbox these three Properties. And since it depends from at least one item in GridView, we must use the MyGridViewItems.Count property for the binding.

<TextBox x:Name="MyTextBox" Text="{Binding MyTextProperty}">
    <TextBox.IsEnabled>
        <MultiBinding Converter="{StaticResource MyMultiPropConverter}">
            <Binding Path="MyGridViewItems.Count"/>
            <Binding Path="MyCheckbox1"/>
            <Binding Path="MyCheckbox2"/>
        </MultiBinding>
    </TextBox.IsEnabled>
</TextBox>

As you can see to handle these three property bindings we need a Converter. For that declare the Converter within the Resources.

<Window.Resources>
    <local:MyMultiPropConverter x:Key="MyMultiPropConverter"/>
</Window.Resources>

Don’t forget to define the namespace (here: local) in your xaml file.

Create suitable converter class inherited from IMultiValueConverter

Now we must create a suitable converter class. The converter class inherited from IMultiValueConverter and implements Convert and ConvertBack methods.

public class MyMultiPropConverter: IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        int count = (int)values[0];
        bool check1 = (bool)values[1];
        bool check2 = (bool)values[2];
        if ((count > 0) || check1 || check2)
        {
            return true;
        }
        else
        {
                return false;
        }
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return null;
    }
}

As you can see the bound properties are represented with an object-array. It is important to pay attention to the order of the items in that array when casting the values to the correct datatype. It has exactly the same order as we have defined above within the xaml-file.

In our example the binding of “IsEnabled” is a one-way binding. For that reasons we don’t really need the ConvertBack method.

Conclusion

Now we are able to bind several properties to another. And we are free to program any logic and combination of these various properties within a converter class.

Leave a Reply

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