Advertise with us

Data Trigger conditions check in WPF

Posted by  Pravesh Singh
WPF 
WPF
 5013  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. 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");
        }
    }

      Modified On Apr-04-2018 01:14:45 AM

Answer