15

We can check some control's string property which has been empty like following code:

<Trigger SourceName="atCaption" Property="Text" Value="{x:Static sys:String.Empty}">
    <Setter TargetName="imgBack" Property="Margin" Value="0"/>
    <Setter TargetName="atCaption" Property="Margin" Value="0"/>
</Trigger>

but, how can one define a condition which is based on a 'not empty' string?

<!--unfortunately, can't accept '!=' operator in xaml.-->
<Trigger SourceName="atCaption" Property="Text" Value!="{x:Static sys:String.Empty}">
    <Setter TargetName="imgBack" Property="Margin" Value="0"/>
    <Setter TargetName="atCaption" Property="Margin" Value="0"/>
</Trigger>
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
mjk6026
  • 1,484
  • 3
  • 20
  • 38
  • similar question here: http://stackoverflow.com/questions/356194/wpf-datatrigger-where-value-is-not-null – LPL Aug 06 '11 at 21:44

4 Answers4

8

to augment the answer by WPF-it (to me this is a permanent solution, not a quick fix)

    <DataTrigger Binding="{Binding VolumeGroup}" Value="{x:Null}">
        <Setter Property="Background" Value="{StaticResource DataGridBackground}" />
    </DataTrigger>
    <DataTrigger Binding="{Binding VolumeGroup}" Value="">
        <Setter Property="Background" Value="{StaticResource DataGridBackground}" />
    </DataTrigger>
</Style.Triggers>
<!--inverted rare case: VolumeGroup will usually be empty so cells will be {StaticResource DataGridBackground}-->
<Setter Property="Background" Value="DarkOliveGreen" />
Tim Richards
  • 89
  • 1
  • 1
6

Using a ValueConverter is a solution.

When using MVVM you could consider an extra property on the ViewModel class you are binding to that determines how a control should be displayed.

When I use the MVVM-way of solving this I don't need a trigger, I simply add extra properties to the ViewModel and bind the View's properties to these extra properties to manipulate the View

Emond
  • 50,210
  • 11
  • 84
  • 115
  • 4
    The only thing I would add about that is that with MVVM, the idea is that the view model should not know how it's displayed. Instead, the view should take the view model and display it however it deems appropriate. e.g. having a colour property on your VM to tell the view what colour to display this thing is not good from a pure MVVM point of view. (Obviously in the real world, 'pure' programming models tend to break down a little, just thought I'd point it out ;)) – Steve Nov 26 '12 at 15:32
  • 2
    @user1080084 - Yes but I have to say that I dislike ValueConverters too: it's code in the view and has to be applied by the designer. These converters 'hang' between View and ViewModel. – Emond Nov 27 '12 at 05:21
6

To quickly get around with thus, the values that apply to the reverse condition should be defaulted in the element declaration or the Style and then use the straight equality condition to alter values.

e.g.

Assume if margin 5 is what you set for empty string and 0 is what you have to set for non empty string then you will set 0 by default as a simple Setter in Style and then check for empty string using Trigger and set 5. Make sure that the default Setter (for 0) appears before Trigger (for 5) in the Style.

WPF-it
  • 19,625
  • 8
  • 55
  • 71
0

If you use a data trigger it uses a binding syntax so you can use a IValueConverter class to convert to property into a boolean value. You can write the check that you want to take place in code inside a custom IValueConverter.

scptre
  • 334
  • 1
  • 9