Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use data trigger in case of value not equal to

I have a Column 'Delivery Boy' in a data grid which is bind with a list in code behind. Values of DataGrid change dynamically.

<DataTrigger Binding="{Binding Path=delivery_boy}" Value="Not Assigned">
   <Setter Property="Foreground" Value="DarkRed"/>
   <Setter Property="Background" Value="Transparent"/>
</DataTrigger>

I can use 'Value' equals to in data trigger, how can I use data trigger for a case where I want the 'Value' property to be 'not equal to'

<DataTrigger Binding="{Binding Path=delivery_boy}" Value!="Not Assigned">
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Background" Value="Green"/>
</DataTrigger>
like image 447
Ghanshyam Kumar Avatar asked Aug 08 '18 12:08

Ghanshyam Kumar


2 Answers

Don't know why this is being voted down, it's a perfectly valid question.

In general you do this by assigning default Setters in a Style and then only using DataTriggers to override these in response to specific values, ie:

<Style>
    <Setter Property="Foreground" Value="White"/>  <!-- Default value -->
    <Setter Property="Background" Value="Green"/>  <!-- Default value -->

    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=delivery_boy}" Value="Not Assigned">
            <Setter Property="Foreground" Value="DarkRed"/>
            <Setter Property="Background" Value="Transparent"/>
        </DataTrigger>
    </Style.Triggers>

</Style>
like image 116
Mark Feldman Avatar answered Nov 16 '22 16:11

Mark Feldman


There is no != operator available in XAML so you will have to use a converter or you could add and bind to another source property where you implement the logic, e.g.:

<DataTrigger Binding="{Binding IsAssigned}" Value="True">
...

public bool IsAssigned => delivery_boy != "Not Assigned";
like image 3
mm8 Avatar answered Nov 16 '22 16:11

mm8