WPF Trigger not null

18,273

Solution 1

Unfortunately, you can't. But actually it's not necessary : you just need to specify the background for when the value is not null in the style setters, not in the trigger :

<Style.Setters>
    <!-- Background when value is not null -->
    <Setter Property="Background" Value="Blue" />
</Style.Setters>
<Style.Triggers>
    <DataTrigger Binding="{Binding}" Value="{x:Null}">
      <Setter Property="Background" Value="Yellow" />
    </DataTrigger>
</Style.Triggers>

Solution 2

You can use DataTrigger class in Microsoft.Expression.Interactions.dll that come with Expression Blend.

Code Sample:

<i:Interaction.Triggers>
    <ie:DataTrigger Binding="{Binding YourProperty}" Value="{x:Null}" Comparison="NotEqual">
       <ie:ChangePropertyAction PropertyName="YourTargetPropertyName" Value="{Binding YourValue}"/>
    </ie:DataTrigger>
</i:Interaction.Triggers>

Using this method you can trigger against GreaterThan and LessThan too. In order to use this code you should reference two dll's:

System.Windows.Interactivity.dll
Microsoft.Expression.Interactions.dll

And add the corresponding namespaces:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"  
xmlns:ie="http://schemas.microsoft.com/expression/2010/interactions"
Share:
18,273
theSpyCry
Author by

theSpyCry

Updated on June 04, 2022

Comments

  • theSpyCry
    theSpyCry 11 months

    How to trigger an action in WPF when the Property is not null? This is a working solution when is null:

    <Style.Triggers>
        <DataTrigger Binding="{Binding}" Value="{x:Null}">
          <Setter Property="Background" Value="Yellow" />
        </DataTrigger>
    </Style.Triggers>
    

    I know that you cant "turn around" the condition and do what you need, but want to know