Using binding for the Value property of DataTrigger condition Using binding for the Value property of DataTrigger condition wpf wpf

Using binding for the Value property of DataTrigger condition


No, it is not possible. As the error message says, only dependency properties can be targets of WPF bindings, and DataTrigger.Value is not a dependency property. So you will need to assign an actual value.

The workaround is to use a MultiBinding whose child Bindings are the two bindings you want to compare, with an IMultiValueConverter which returns true if the two inputs are equal and false if they are unequal. The DataTrigger can then use that MultiBinding, and a Value of True.


Here is an example with the IMultiValueConverter converter.

In this XAML there are three check boxes. The first two are binded to the Foo and Bar properties (both are boolean). The third one uses multi binding with the IMultiValueConverter. It is checked when Foo and Bar have the same values.

<!-- It's expected that the DataContext of this StackPanel has boolean Bar and Foo properties.. --><StackPanel Orientation="Vertical">    <StackPanel.Resources>        <!-- local contains the MultiValueEqualityConverter class implementation -->        <local:MultiValueEqualityConverter x:Key="multiValueEqualityConverter"/>    </StackPanel.Resources>    <CheckBox IsChecked="{Binding Foo}">Foo</CheckBox>    <CheckBox IsChecked="{Binding Bar}">Bar</CheckBox>    <CheckBox IsEnabled="False" Content="Are the same">        <CheckBox.Style>            <Style TargetType="CheckBox">                <Style.Setters>                    <Setter Property="IsChecked" Value="False"/>                </Style.Setters>                <Style.Triggers>                    <DataTrigger Value="True">                        <DataTrigger.Binding>                            <MultiBinding Converter="{StaticResource multiValueEqualityConverter}">                                <Binding RelativeSource="{RelativeSource self}" Path="DataContext.Foo" Mode="OneWay" />                                <Binding RelativeSource="{RelativeSource self}" Path="DataContext.Bar" Mode="OneWay"/>                            </MultiBinding>                        </DataTrigger.Binding>                        <Setter Property="IsChecked" Value="True" />                    </DataTrigger>                </Style.Triggers>            </Style>        </CheckBox.Style>    </CheckBox></StackPanel>

Simple IMultiValueConverter implementation for one way binding:

public class MultiValueEqualityConverter : IMultiValueConverter{    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)    {        return values?.All(o => o?.Equals(values[0]) == true) == true || values?.All(o => o == null) == true;    }    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)    {        throw new NotImplementedException();    }}


The Value property of the DataTrigger is not a DependencyProperty that can be bind. Therefore, we RegisterAttached a dependency property that can bind and set the value of DataTrigger's Value property each time the attached dependency property value is set.

Here is an example DataTriggerAssists class

public class DataTriggerAssists{    public static readonly DependencyProperty ValueProperty =        DependencyProperty.RegisterAttached(            "Value",            typeof(object),            typeof(DataTriggerAssists),            new FrameworkPropertyMetadata(null, OnValueChanged));    public static object GetValue(DependencyObject d)    {        return d.GetValue(ValueProperty);    }    public static void SetValue(DependencyObject d, object value)    {        d.SetValue(ValueProperty, value);    }    public static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)    {        if (d is DataTrigger trigger)            trigger.Value = args.NewValue;    }}

Declare a prefix named as with the namespace of the DataTriggerAssists class.

Then you can use like this.

<DataTrigger Binding="{Binding Foo}"              as:DataTriggerAssists.Value="{Binding ElementName=AnotherElement, Path=Bar}">..