Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple binding to IsEnable

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>
like image 240
Keith Nelson Avatar asked Sep 06 '12 19:09

Keith Nelson


3 Answers

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.

like image 74
Pavel Voronin Avatar answered Nov 10 '22 04:11

Pavel Voronin


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!

like image 6
rrhartjr Avatar answered Nov 10 '22 03:11

rrhartjr


Bind IsEnabled using a MultiBinding

like image 1
GazTheDestroyer Avatar answered Nov 10 '22 05:11

GazTheDestroyer