Search code examples
.netwpfdata-bindingdatatrigger

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>

Solution

  • 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";