Question

I have MultiDataTrigger looks like this:

<Style x:Key="btnStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource BaseBtnStyle}">
    <Setter Property="UIElement.IsEnabled" Value="False" />
    <Setter Property="Margin" Value="3,2"/>
    <Setter Property="Template" Value="{DynamicResource GrayButtonTemplate}"/>

    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding Num}" Value="1"/>
                <Condition Binding="{Binding Str}"  Value="aaa"/>
            </MultiDataTrigger.Conditions>

            <Setter Property="UIElement.IsEnabled" Value="True" />
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

The goal is that the button will be enabled as soon as str = aaa and Num = 1.

It works.

The problem is that I want the button will be enabled even when Num = 2 (and str = aaa)

I tried to add more MultiDataTrigger after the first:

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding Num}" Value="2"/>
        <Condition Binding="{Binding Str}"  Value="aaa"/>
    </MultiDataTrigger.Conditions>

    <Setter Property="UIElement.IsEnabled" Value="True" />
</MultiDataTrigger>

He did not refer to it, the button is only enabled when Num = 1.

There is another way to do this?

Was it helpful?

Solution

the easiest solution I can see is to create IsEnabled property in your object, where you have Str and Num

public bool IsEnabled
{
   get
   {
      return (Num == 1 || Num == 2) && Str == "aaa";
   }
}

bind IsEnabled in your Style

<Setter Property="IsEnabled" Value="{Binding Path=IsEnabled}"/>

and whenever Str or Num changes notify about IsEnabled change as well

OTHER TIPS

Declare a binding for IsEnabled property with converter,

    <Style x:Key="btnStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource BaseBtnStyle}">
        <Setter Property="UIElement.IsEnabled" Value="False" />
        <Setter Property="Margin" Value="3,2"/>
        <Setter Property="Template" Value="{DynamicResource GrayButtonTemplate}"/>
        <Setter Property="IsEnabled" Value="{Binding Converter={StaticResource myconverter}}"/>
    </Style>

Since we didn't specify the path for the Binding, entire data context object will pass through the converter. Based on several conditions, you can return true or false, that will enable or disable the button.

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var foo = (foo) value;
        return (foo.num == 1 || foo.num == 2) && foo.str == "aaa";
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top