2

I have one int value in my Vmodel :

public int MaxTagCount => URLsCount.Max(tag => tag.Count);

And I need to connect this MaxTagCount with Trigger :

 <DataTrigger Binding="{Binding Count}" Value="1149">
     <Setter Property="FontWeight" Value="Bold"/>
     <Setter Property="Foreground" Value="Green"/>
 </DataTrigger>

How can I replace "1149" to MaxTagCount ?

Сергей
  • 780
  • 4
  • 13
  • 31
  • Does this answer your question? [Using binding for the Value property of DataTrigger condition](https://stackoverflow.com/questions/2240421/using-binding-for-the-value-property-of-datatrigger-condition) – StayOnTarget May 13 '22 at 19:47

2 Answers2

1

If i understand you correctly, you are looking for a way to bind the Value of the DataTrigger to your MaxTagCount property, which isn't possible due to the fact that Value isn't a dependency property.

The most common workaround that is to pass both the MaxTagCount property and the Count property to a MultiValueConverter, the converter will compare those two values and return true or false. The role of the DataTrigger now will be to check the value returned by the converter so:

First, define a basic converter that compares two values like so:

public class CompareValuesConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values?[0].Equals(values[1]);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Second, update your DataTrigger to check the returned value of the converter, and pass your values to the converter, and set your style accordingly:

<DataTrigger Value="True">
                <DataTrigger.Binding>
                    <MultiBinding >
                        <MultiBinding.Converter>
                            <local:CompareValuesConverter/>
                        </MultiBinding.Converter>
                        <Binding Path="Count" />
                        <Binding Path="DataContext.MaxTagCount" ElementName="Main"/>
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Foreground" Value="Green"/>
</DataTrigger>

Notice that i am using an ElementName binding to get the MaxTagCount value since it is (most likely) defined on the global UI DataContext (in this case the main Window is Named Main), you could also use a RelativeSource binding.

SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47
1

As already explained by @Elhamer, you can't bind to the Value property of a DataTrigger because it is not a dependency property.

As an alternative to using a multi converter, you could just add another property to your view model that returns a bool that indicates whether the Count and MaxTagCount properties are equal:

public bool IsMax => Count == MaxCount;

...and bind to this one:

<DataTrigger Binding="{Binding IsMax}" Value="True">
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Foreground" Value="Green"/>
</DataTrigger>

After all, a view model is nothing but a model for the view and this kind of logic makes perfect sense to implement there.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • this solution is very interesting, but if the `Max` are even always are true, trigger not work. – Сергей Feb 19 '18 at 18:05
  • My bool property is = `public bool IsMax = true` and trigger like in your answer, but `DataTrigger` does not work – Сергей Feb 19 '18 at 18:09
  • This answer is somewhat incomplete and that’s probably why it’s not working for you. You also need to fire the PropertyChanged event for the IsMax property whenever either Count or MaxCount changes. – Dave M Feb 19 '18 at 23:46
  • Do you implement the INotifyPropertyChanged event in your view model? – mm8 Feb 20 '18 at 15:03