How to use data trigger in case of value not equal to
Asked Answered
S

2

21

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>
Shortwave answered 8/8, 2018 at 12:31 Comment(1)
Use a Binding Converter that returns an appropriate boolean value.Skimpy
M
4

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";
Meyeroff answered 8/8, 2018 at 13:28 Comment(0)
M
20

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>
Mauricio answered 8/8, 2018 at 13:24 Comment(0)
M
4

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";
Meyeroff answered 8/8, 2018 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.