WPF trigger when property and data value is true

I need to change the Stylecontrol when the property and data value is true. For example, my related data has a property IsDirty. I would like to change the background color of my control when IsDirtytrue AND the control is selected. I found classes MultiTriggerand MultiDataTrigger... but in this case I need to somehow call the data and property. How can i do this?

Another note: I need to be able to do this in code, not in XAML.

+3
source share
2 answers

MultiDataTrigger works just as well for DependencyProperties as it does for regular properties. Just set the path to the name of your dependency property.

You need to be careful when setting the source of this binding, since the default source is the DataContext of the element to which the trigger is connected. If a trigger is used in style on a selectable object, you can use the RelativeSource property to bind:

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
      <Condition Binding="{Binding Path=IsDirty}" Value="True" />
      <Condition Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}" Value="True" />
    </MultiDataTrigger.Conditions>
    <Setter Property="Background" Value="Cyan" />
  </MultiDataTrigger>
+9
source

Here's how I actually did it in code:

new MultiDataTrigger
{
    Conditions = 
    {
        new Condition
            {
                Binding = new Binding("IsDirty"),
                 Value = true
            },
        new Condition
        {                                                    
            Binding = new Binding("IsSelected"){RelativeSource = RelativeSource.Self},
            Value = true
        }
    },

    Setters =
    {
        new Setter
        {
            Property = Control.BackgroundProperty,
             Value = Brushes.Pink,
        },
     }
}
0
source

Source: https://habr.com/ru/post/1747375/


All Articles