Home > OS >  How to bind IsEnabled with inline expression?
How to bind IsEnabled with inline expression?

Time:09-16

Let's say I have

<TextBox
    x:Name="txtBox1" />

<!-- more XAML here -->

<TextBox
   x:Name="associatedToTextBox1"
   IsEnabled={Binding ElementName=txtBox1, Path=Text, Magic=txtBox.Text != string.Empty} />

I want associatedToTextBox1 to be enabled only when txtBox1 is not empty. I thought there was a way to embed that functionality into xaml, without converters. Is this possible? If so, how?

CodePudding user response:

There is nothing like an "inline expression".

You may however use a DataTrigger in a TextBox Style:

<TextBox x:Name="associatedToTextBox1">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Text, ElementName=txtBox1}"
                             Value="">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
  • Related