WPF 按钮已启用 检查多个绑定

本文关键字:绑定 检查 启用 按钮 WPF | 更新日期: 2023-09-27 17:55:41

我当前具有使用以下数据绑定配置的 XAML 按钮的 IsEnabled 属性:

<Button Name="ThirdPartyPostoneButton" Content="Postpone"
        Click ="postponeThirdPartyUpdatesButton_Click" Margin="5,5,0,0"
        Height="25" IsEnabled="{Binding Item3.CanDefer}"/>

我还需要添加对IsEnabled="{Binding Item3.InstallSourceExists}"的检查(换句话说,必须满足这两个条件才能启用按钮)。我怎样才能做到这一点?

WPF 按钮已启用 检查多个绑定

我能想到的两个选项:-

  1. 使用多重绑定和自定义IMultiValueConverter来检查这两个值是否为真。

  2. 在"Item3"模型上公开一个新属性,如果其他属性均为 True,则仅返回 true。这是一种更简洁的方法,意味着如果将来逻辑发生更改(例如,需要包含第三个布尔属性),则不必接触 XAML。

使用如下所示的多重绑定:

<Button Name="ThirdPartyPostoneButton" Content="Postpone" Click ="postponeThirdPartyUpdatesButton_Click" Margin="5,5,0,0" Height="25" >
  <Button.IsEnabled>
    <MultiBinding Converter="{StaticResource MyCustomConvertor}">
      <Binding Path="Item3.CanDefer"/>
      <Binding Path="Item3.InstallSourceExists"/>
    </MultiBinding>
  </Button.IsEnabled>
</Button>