【问题标题】:formulas in WPF bindingsWPF 绑定中的公式
【发布时间】:2012-02-20 11:00:45
【问题描述】:

我有一个组合框,我希望在未选中复选框时启用它。我该怎么写?我尝试了以下操作,但 WPF 似乎无法识别此语法:

<ComboBox IsEnabled={Binding Path=!CheckBoxIsChecked, Mode=OneWay}/>
<CheckBox IsChecked={Binding Path=CheckBoxIsChecked}/>

【问题讨论】:

标签: .net wpf binding


【解决方案1】:

您必须编写一个转换器,即一个实现IValueConverter 接口的类。然后将转换器分配给您绑定的 Converter 属性:

<ComboBox IsEnabled="{Binding Path=CheckBoxIsChecked, Mode=OneWay, Converter={StaticResource MyConverter}}"/> 

【讨论】:

  • 写一个属性 CheckBoxIsNotChecked { get { return !CheckBoxIsChecked; } }
  • 是的,但是如果您绑定到一个不是您自己编写的对象,即您无法控制属性集的对象,该怎么办?
【解决方案2】:

你必须使用转换器来实现这一点。

public class BooleanNegationConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ConvertValue(value);
    }
    private bool ConvertValue(object value)
    {
        bool boolValue;
        if(!Boolean.TryParse(value.ToString(), out boolValue))
        {
            throw new ArgumentException("Value that was being converted was not a Boolean", "value");
        }
        return !boolValue;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ConvertValue(value);
    }
}

然后像这样使用它:

<ComboBox IsEnabled="{Binding Path=CheckBoxIsChecked, 
                              Mode=OneWay, 
                              Converter={StaticResource BooleanNegationConverterKey}}"/>

请记住,您必须在 xaml 资源中声明此静态资源。像这样:

<UserControl.Resources>
    <ResourceDictionary>
        <BooleanNegationConverter x:Key="BooleanNegationConverterKey" />
    </ResourceDictionary>
</UserControl.Resources>

【讨论】:

    【解决方案3】:

    你应该使用所谓的转换器来做这些事情。

    {Binding ElementName=CheckBox, Path=IsChecked, Converter=BoolToVisibilityConverter}
    

    BoolToVisibilityConverter 是一个标准的 WPF 转换器。您还可以轻松编写 OppositeBoolToVisibilityConverter。网上很多例子。

    【讨论】:

      【解决方案4】:

      触发器应该同样适用:

         <CheckBox IsChecked="{Binding Path=CheckBoxIsChecked}" />
          <ComboBox Grid.Row="1" ItemsSource="{Binding Path=ComboItems}" SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}">
              <ComboBox.Style>
                  <Style TargetType="ComboBox">
                      <Style.Triggers>
                          <DataTrigger Binding="{Binding Path=CheckBoxIsChecked}" Value="False" >
                              <Setter Property="IsEnabled" Value="True"/>
                          </DataTrigger>
                          <DataTrigger Binding="{Binding Path=CheckBoxIsChecked}" Value="True" >
                              <Setter Property="IsEnabled" Value="False"/>
                          </DataTrigger>
                      </Style.Triggers>
                  </Style>
              </ComboBox.Style>
          </ComboBox>
      

      【讨论】:

      • 不喜欢这个解决方案,因为 sll 已经提到了。对于应用程序中多个位置的多个编辑器而言,它的代码更多且可维护性更低。
      猜你喜欢
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 2011-02-02
      • 2011-08-25
      • 1970-01-01
      • 2013-07-13
      相关资源
      最近更新 更多