I'm working on a WPF application and struggling with a data trigger. I'd like to bind the value of the trigger condition to some object I have:
<DataTrigger Binding="{Binding Foo}"
Value="{Binding ElementName=AnotherElement, Path=Bar}">..
However, I'm not allowed as it doesn't seem to be possible to use bindings for the Value property. Is it? Can I achieve this somehow? I get the following error:
A 'Binding' cannot be set on the 'Value' property of type 'DataTrigger'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
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.
<!-- 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();
}
}
I use MVVM. Since I've returned to this answer quite a few times, it's worth mentioning that each time I end up with the same result:
Make a model for each item rather than comparing it.
For example, I typically have a stack of rows in an ItemsControl.DataTemplate
. I try to set IsEnabled
(or something) using a DataTrigger
by comparing to a dynamic Value={Binding}
.
Right about then, my code belly flops, and I head to SO, and end up here.
Almost without fail, I then decide to maintain a list of row models in the ViewModel, which handle their own IsEnabled
and notify the UI accordingly.
I use them for the ItemsControl.Source
, then wonder why I didn't just do that to begin with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With