2009-06-03 77 views
32

我有一个描述GUI部分的WPF xaml文件,我希望特定控件的启用/禁用依赖于其他两个文件。该代码看起来是这样的时刻:使用多个绑定的C#WPF IsEnabled?

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

但我想它是依赖于另一复选框,以及所以像:

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

什么是去的最佳途径?我不禁感到我错过了明显的东西,或者以错误的方式去做这件事?

回答

54

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

只给一个答案,你可以(几乎)复制&贴:

所需的静态资源:

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

组合框:

<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."); 
     } 
    } 
} 
+0

我有一个问题,如果什么值是空? ,难道不比在开始的时候有这样的检查: `如果(价值== NULL){ 回报 假; }` ? – Devid 2018-01-08 14:41:04

7

您也可以尝试同样的短版:

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

,当然,你可能需要知名度的转换器,太:

public class BooleanOrToVisibilityConverter : IMultiValueConverter 
{ 
    public Visibility HiddenVisibility { get; set; } 

    public bool IsInverted { get; set; } 

    public BooleanOrToVisibilityConverter() 
    { 
     HiddenVisibility = Visibility.Collapsed; 
     IsInverted = false; 
    } 

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     bool flag = values.OfType<IConvertible>().Any(System.Convert.ToBoolean); 
     if (IsInverted) flag = !flag; 
     return flag ? Visibility.Visible : HiddenVisibility; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

public class BooleanAndToVisibilityConverter : IMultiValueConverter 
{ 
    public Visibility HiddenVisibility { get; set; } 

    public bool IsInverted { get; set; } 

    public BooleanAndToVisibilityConverter() 
    { 
     HiddenVisibility = Visibility.Collapsed; 
     IsInverted = false; 
    } 

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     bool flag = values.OfType<IConvertible>().All(System.Convert.ToBoolean); 
     if (IsInverted) flag = !flag; 
     return flag ? Visibility.Visible : HiddenVisibility; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
}