如果这只是你需要的地方,那么使用简单的解决方案就可以了,但我认为在真实应用中,你可能会多次遇到这种情况,所以我更喜欢使用通用解决方案来使事情变得更简单。
对于我来说,我会使用 MultiConverter 来计算从几个其他布尔值使用逻辑 OR
得出的结果布尔值,就像这样:
<Window.Resources>
<converters:MultiBoolOrConverter x:Key="MultiBoolOrConverter" />
</Window.Resources>
<StackPanel>
<RadioButton GroupName="group" x:Name="enabledOne">Enabled one</RadioButton>
<RadioButton GroupName="group" x:Name="enabledTwo">Enabled two</RadioButton>
<RadioButton GroupName="group" x:Name="disabledOne">Disabled one</RadioButton>
<RadioButton GroupName="group" x:Name="disabledTwo">Disabled two</RadioButton>
<TextBox Text="Test">
<TextBox.IsEnabled>
<MultiBinding Converter="">
<Binding Path="IsChecked" ElementName="enabledOne" />
<Binding Path="IsChecked" ElementName="enabledTwo" />
</MultiBinding>
</TextBox.IsEnabled>
</TextBox>
</StackPanel>
使用的是 MultiBoolOrConverter.cs
转换器类:
using System;
using System.Linq;
using System.Globalization;
using System.Windows.Data;
namespace StackOverFlowTest.Converters
{
class MultiBoolOrConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values.Cast<bool>().Any(value => value);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
有时您可能需要使用AND
而不是OR
,这时可以使用MultiBoolAndConverter
转换器:
using System;
using System.Linq;
using System.Globalization;
using System.Windows.Data;
namespace StackOverFlowTest.Converters
{
class MultiBoolAndConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values.Cast<bool>().All(value => value);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
我希望这能帮到你。
更新 ---------------------
在我的以前的解决方案中,我使用了以下逻辑:
如果选择了前两个按钮之一,则应启用控件。
而不是您字面上所要求的:
如果选择了最后两个按钮之一,则应禁用控件。
当然,它确实有效,但如果您想要完全按照您所描述的来做,那么代码将需要进行一些更改,使用转换器来否定 IsChecked 布尔值。
<Window.Resources>
<converters:MultiBoolAndConverter x:Key="MultiBoolAndConverter" />
<converters:BoolNegationConverter x:Key="BoolNegationConverter" />
</Window.Resources>
<StackPanel>
<RadioButton GroupName="group" x:Name="enabledOne">Enabled one</RadioButton>
<RadioButton GroupName="group" x:Name="enabledTwo">Enabled two</RadioButton>
<RadioButton GroupName="group" x:Name="disabledOne">Disabled one</RadioButton>
<RadioButton GroupName="group" x:Name="disabledTwo">Disabled two</RadioButton>
<TextBox Text="Test">
<TextBox.IsEnabled>
<MultiBinding Converter="{StaticResource MultiBoolAndConverter}">
<Binding Path="IsChecked" ElementName="disabledOne" Converter="{StaticResource BoolNegationConverter}" />
<Binding Path="IsChecked" ElementName="disabledTwo" Converter="{StaticResource BoolNegationConverter}" />
</MultiBinding>
</TextBox.IsEnabled>
</TextBox>
</StackPanel>
BoolNegationConverter.cs
:
using System;
using System.Globalization;
using System.Windows.Data;
namespace StackOverFlowTest.Converters
{
class BoolNegationConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(value is bool && (bool)value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(value is bool && (bool)value);
}
}
}