多重绑定到IsEnable

9

我需要绑定一个满足两个条件的TextBox

  • 如果Text.Length > 0,则启用
  • 如果user.IsEnabled为True,则启用

其中user.IsEnabled是从数据源中获取的。我想知道是否有一种简单的方法来实现这个功能。

以下是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>

你想如何组合这两个属性?如果两个都为真或其中一个为真? - rrhartjr
3个回答

7
如GazTheDestroyer所说,您可以使用MultiBinding。 您还可以使用仅限XAML的解决方案使用MultiDataTrigger来完成此操作 但是,您应该切换条件,因为触发器仅支持相等。
<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>

如果其中一个条件不满足,该值将被设置为其默认值或样式中的值。但是不要设置本地值,因为它会覆盖样式和触发器的值。

这将导致这些值的逻辑与。您可以为每个属性简单地拥有两个“触发器”。此外,您需要绑定到TextBoxText.Length而不是Self - rrhartjr
是的,这是AND,但Keith需要OR来处理未切换的变体。要启用TextBox,可以使用user.IsEnabled OR Text.Length > 0。请参见问题下面的注释。 - Pavel Voronin

6

因为您只需要一个逻辑 OR,所以您只需要为每个属性添加两个触发器。

尝试使用以下 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>

我将 DataContext 设置为具有名为 MyIsEnabledDependencyPropertyWindow 类。显然,您需要根据您特定的 DataContext 进行修改。

以下是相关的代码:

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;
}

希望这有所帮助!

1

我尝试过了,但是Multibinding在IsEnable上不可用,除非我做错了什么。 - Keith Nelson
1
IsEnabled,不是IsEnable?你只是在绑定时打错了吗?请在你的问题中添加一些XAML。 - Pavel Voronin
这是我目前的做法。但我不想用 ContentControl 包装每个控件。我尝试了设置上面展示的样式示例,但那会破坏我正在使用的主题。由于我仍在学习 WPF,我想知道有哪些选项可用。 - Keith Nelson
<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> - Keith Nelson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接