Home > DeveloperSection > Forums > Data Trigger conditions check in WPF
Pravesh Singh
Pravesh Singh

Total Post:101

Points:709
Posted on    December-27-2013 7:01 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 3119  View(s)
Rate this:

If any possible to give condition on DataTrigger?

<DataTrigger Binding="{Binding MessageBoxImage}" Value="{x:Static MessageBoxImage.Error}">

    <Setter Property="Source" Value="../Images/Error48.png"></Setter>

</DataTrigger>

<DataTrigger Binding="{Binding MessageBoxImage}" Value="{x:Static MessageBoxImage.Hand}">

    <Setter Property="Source" Value="../Images/Error48.png"></Setter>

</DataTrigger>

<DataTrigger Binding="{Binding MessageBoxImage}" Value="{x:Static MessageBoxImage.Stop}">

    <Setter Property="Source" Value="../Images/Error48.png"></Setter>

</DataTrigger>

So, this is my Xaml code, in that Error,Hand,Stop all are setting same image

My question is any possible to give OR condition for these three values? (or one line statement)

Thanks



Alex Leblois
Alex Leblois

Total Post:67

Points:471
Posted on    December-27-2013 7:43 AM

Hi Pravesh,

You can use MultiDataTrigger for AND condition. As for OR condition you can use converter.

<Window.Resources>

   <someNs:ORConverter x:Key = "ORConverter"/>

</Window.Resources>

....

<DataTrigger Value="True">

   <DataTrigger.Binding>

        <Binding Path="MessageBoxImage" Converter="{StaticResource ORConverter}">

            <Binding.ConverterParameter>

                <x:Array Type="MessageBoxImage">

                     <x:Static MemberType="MessageBoxImage" Member="Error" />

                     <x:Static MemberType="MessageBoxImage" Member="Information" />

                     <x:Static MemberType="MessageBoxImage" Member="Question" />

                </x:Array>

            </Binding.ConverterParameter>

        </Binding>

   </DataTrigger.Binding>

   <Setter Property="Source" Value="../Images/Error48.png"></Setter>

</DataTrigger>

And the converter's code:

public class ORConverter : IValueConverter

{

    public object Convert(object value, Type targetType,

           object parameter, System.Globalization.CultureInfo culture)

    {

         var list = parameter as IList;

         if (list == null)

             return false;

         foreach (var o in list)

         {

             if (Equals(o, value))

                return true;

         }

         return false;

    }

    public object ConvertBack(object value, Type targetType,

           object parameter, System.Globalization.CultureInfo culture)

    {

        throw new NotSupportedException("Cannot convert back");

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick