multiple binding to IsEnable

Keith Nelson picture Keith Nelson · Sep 6, 2012 · Viewed 9.6k times · Source

I need to bind a TextBox that meets two criteria:

  • IsEnabled if Text.Length > 0
  • IsEnabled if user.IsEnabled

Where user.IsEnabled is pulled from a data source. I was wondering if anyone had a easy method for doing this.

Here is the XAML:

<ContentControl IsEnabled="{Binding Path=Enabled, Source={StaticResource UserInfo}}"> 
    <TextBox DataContext="{DynamicResource UserInfo}" Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding Path=Text, RelativeSource={RelativeSource Self}, Converter={StaticResource LengthToBool}}"/> 
</ContentControl>

Answer

Pavel Voronin picture Pavel Voronin · Sep 6, 2012

As GazTheDestroyer said you can use MultiBinding.

You can also acomplish this with XAML-only solution using MultiDataTrigger

But you should switch the conditions cause triggers support only equality

<Style.Triggers>  
  <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text.Length}" Value="0" />
          <Condition Binding="{Binding Source=... Path=IsEnabled}" Value="False" />
        </MultiDataTrigger.Conditions>
        <Setter Property="IsEnabled" Value="False" />
      </MultiDataTrigger>  
</Style.Triggers>

If one of the condition is not met the value be set to its default or value from the style. But do not set local value as it overrides style's and trigger's values.