Posted by: Zeeshan Amjad | June 29, 2010

Negation condition in Data Trigger


We already saw few example of data trigger here and here. But sometimes we want to execute the negative condition. Such as trigger should execute when the value of data is not equal to your given value. WPF doesn’t have any direct support of it, but we can do this with the help of data converter. We create a simple converter, that takes Boolean as an input and also return Boolean output. But the whole purpose of this converter is to reverse the input value. i.e. if input is true then it will return false and vice versa. Here is our converter.

    [ValueConversion(typeof(bool), typeof(bool))]
    public class NegationConvertor : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                Boolean condition = (Boolean)value;

                return !condition;
            }
            else
                return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                Boolean condition = (Boolean)value;

                return !condition;
            }
            else
                return value;
        }
    }

Now we are going to create its object in XAML and use this when binding the data with data trigger. Here is complete XAML code of the program.

<Window x:Class="TypeConvertor.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TypeConvertor"
    Title="Data Trigger Demo" Height="300" Width="400" Loaded="Window_Loaded">    
    <Window.Resources>
        <local:NegationConvertor x:Key="objNegationConvertor"/>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsCapital, Converter={StaticResource objNegationConvertor}}" Value="true">
                    <Setter Property="Background" Value="Green"/>
                    <Setter Property="Foreground" Value="White"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    
    <Grid>                
        <ListView Name="lstView">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="180" Header="State" DisplayMemberBinding="{Binding Path = State}"/>
                    <GridViewColumn Width="180" Header="City" DisplayMemberBinding="{Binding Path = City}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

In this XAML, we define the trigger that if the city is not capital of state then change its foreground color to white and background color to green. Here is complete C# code of this program.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Reflection;
using System.Globalization;
using System.Windows.Data;

namespace TypeConvertor
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            lstView.Items.Add(new CityInfo("California", "Los Angeles", false, true));
            lstView.Items.Add(new CityInfo("California", "San Francisco", false, false));
            lstView.Items.Add(new CityInfo("California", "Sacramento", true, false));
            lstView.Items.Add(new CityInfo("California", "San Diego", false, false));            
            lstView.Items.Add(new CityInfo("Massachusetts", "Boston", true, true));
            lstView.Items.Add(new CityInfo("Massachusetts", "Newton", false, false));
        }
    }

    public class CityInfo
    {
        public CityInfo()
        {
        }

        public CityInfo(String state, String city,
            bool isCapital, bool isLargest)
        {
            State = state;
            City = city;
            IsCapital = isCapital;
            IsLargest = isLargest;
        }

        public String State
        { set; get; }

        public String City
        { set; get; }

        public bool IsCapital
        { set; get; }

        public bool IsLargest
        { set; get; }
    }

    [ValueConversion(typeof(bool), typeof(bool))]
    public class NegationConvertor : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                Boolean condition = (Boolean)value;

                return !condition;
            }
            else
                return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                Boolean condition = (Boolean)value;

                return !condition;
            }
            else
                return value;
        }
    }
}

Here is the output of the program.

NegationDataTrigger


Responses

  1. […] have already saw an example of Negative converter here. Now we are going one step further and see how can we make a Range converter. The obvious thing […]

  2. That solves my problem. Thank you.

    • I am glad that it solves your problem. Thanks for visiting and like it.
      Best regards
      Zeeshan Amjad

  3. […] already have an example of not converter here. There is only one operand to not operator, therefore we can implement it using IValueConverter […]


Leave a comment

Categories