Question

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>
Was it helpful?

Solution

Since you only need a logical OR, you just need two Triggers to your each of the properties.

Try this XAML:

<StackPanel>
        <StackPanel.Resources>
            <Style TargetType="{x:Type Button}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=InputText, Path=Text}" Value="" >
                        <Setter Property="IsEnabled" Value="False" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=MyIsEnabled}" Value="False" >
                        <Setter Property="IsEnabled" Value="False" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </StackPanel.Resources>
        <StackPanel Orientation="Horizontal">
            <Label>MyIsEnabled</Label>
            <CheckBox IsChecked="{Binding Path=MyIsEnabled}" />
        </StackPanel>
        <TextBox Name="InputText">A block of text.</TextBox>
        <Button Name="TheButton" Content="A big button.">     
        </Button>
    </StackPanel>

I set DataContext to the Window class which has a DependencyProperty called MyIsEnabled. Obviously you would have to modify for your particular DataContext.

Here is the relevant code-behind:

public bool MyIsEnabled
{
    get { return (bool)GetValue(IsEnabledProperty); }
    set { SetValue(IsEnabledProperty, value); }
}

public static readonly DependencyProperty MyIsEnabledProperty =
    DependencyProperty.Register("MyIsEnabled", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(true));


public MainWindow()
{
    InitializeComponent();
    this.DataContext = this;
}

Hope that helps!

OTHER TIPS

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.

Bind IsEnabled using a MultiBinding

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top