如果這是您需要它的唯一地方,那麼可以使用您的簡單解決方案,但我認爲在實際應用中,您可能會多次面對這種情況,所以我更喜歡使用通用解決方案來製作東西簡單。 對於我來說,我會用多轉換到使用邏輯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="{StaticResource MultiBoolOrConverter}">
<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();
}
}
}
我希望這會幫助你。
UPDATE ---------------------
在我以前的解決方案,我用的邏輯:
The control should be enabled if one of the first two buttons are selected.
而不是你有什麼要求字面上:
The control should be disabled if one of the last two buttons are selected.
據工作過程中,但如果你想它完全按照你描述的那麼代碼需要變化不大使用轉換否定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);
}
}
}
這將是我的首選解決方案好。使用多值轉換器可以將這種行爲擴展到無限數量的布爾值 – Patrick
這實際上比我的更簡潔。正如你所說在更多情況下它是可用的。感謝您的輸入! – FishySwede
不客氣,我很高興它對你有用。 – Amer