C#WPF IsEnabled使用多个绑定?

时间:2009-06-03 15:32:39

标签: c# wpf binding combobox isenabled

我有一个描述GUI部分的WPF xaml文件,我希望特定控件的启用/禁用依赖于其他两个控件。目前代码看起来像这样:

<ComboBox Name="MyComboBox"
          IsEnabled="{Binding ElementName=SomeCheckBox, Path=IsChecked}"/>

但是我希望它依赖于另一个复选框,例如:

<ComboBox Name="MyComboBox"
          IsEnabled="{Binding ElementName=SomeCheckBox&AnotherCheckbox, Path=IsChecked}"/>

最好的方法是什么?我情不自禁地觉得我错过了一些明显的事情,或者说这是错误的方式?

4 个答案:

答案 0 :(得分:68)

您可以将MultiBinding与实现IMultiValueConverter的转换器一起使用。

只需给出答案即可(几乎)复制和粘贴:

需要静态资源:

<converterNamespace:BooleanAndConverter x:Key="booleanAndConverter" />

ComboBox:

<ComboBox Name="MyComboBox">
  <ComboBox.IsEnabled>
    <MultiBinding Converter="{StaticResource booleanAndConverter}">
      <Binding ElementName="SomeCheckBox" Path="IsChecked" />
      <Binding ElementName="AnotherCheckbox" Path="IsChecked"  />
    </MultiBinding>
  </ComboBox.IsEnabled>
</ComboBox>

转换器的代码:

namespace ConverterNamespace
{
    public class BooleanAndConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            foreach (object value in values)
            {
                if ((value is bool) && (bool)value == false)
                {
                    return false;
                }
            }
            return true;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("BooleanAndConverter is a OneWay converter.");
        }
    }
}

答案 1 :(得分:14)

你也可以试试相同的更短版本:

var button = document.getElementById("Sailthru");

button.addEventListener("click", function(){
    document.location.href = 'http://www.sailthru.com';
});

当然,您也可能需要转换器以提高可见性:

public class BooleanAndConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return values.OfType<IConvertible>().All(System.Convert.ToBoolean);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

public class BooleanOrConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return values.OfType<IConvertible>().Any(System.Convert.ToBoolean);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

答案 2 :(得分:13)

我相信您可能必须使用MultiBinding与MultiValueConverter。见这里:http://www.developingfor.net/wpf/multibinding-in-wpf.html

以下是一个直接相关的示例:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5b9cd042-cacb-4aaa-9e17-2d615c44ee22

答案 3 :(得分:1)

作为qqbenq答案的扩展:

添加了用于处理集合Count的功能,例如,如果您要检查是否选择了ListView的某些项目。

转换器:

public class IsEnabledConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        foreach (var value in values)
        {
            switch (value)
            {
                case bool b when !b:
                case int i when i == 0:
                    return false;
            }
        }

        return true;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return null;
    }
}

命名空间 <theNamespace:IsEnabledConverter x:Key="IsEnabledConverter"/>

按钮

<Button x:Name="MyButton">
    <Button.IsEnabled>
        <MultiBinding Converter="{StaticResource IsEnabledConverter}">
            <Binding ElementName="MyListView" Path="SelectedItems.Count"/>
            <Binding ElementName="MyCheckBox" Path="IsChecked"/>
        </MultiBinding>
    </Button.IsEnabled>
</Button>