DATA TRIGGER CONDITIONS CHECK IN WPF

Pravesh Singh

Total Post:101

Points:709
Posted by  Pravesh Singh
WPF 
WPF
 4049  View(s)
Ratings:
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

  1. Alex Leblois

    Post:67

    Points:471
    Re: Data Trigger conditions check in WPF

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

        }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!