Fis*_*ede 4 c# wpf xaml radio-button
我的问题是我有一组RadioButtons,我想根据选择的RadioButton禁用另一个控件.
重要的是,如果选择了几个 RadioButton中的一个,我可以禁用控制.
例如
[] Enabled one
[] Enabled two
[] Disabled one
[] Disabled two
Run Code Online (Sandbox Code Playgroud)
如果选择了最后两个按钮之一,则应禁用该控件.
注意
如果这是你需要它的唯一地方,那么可以使用你的简单解决方案,但我认为在实际的应用程序中你可能会多次面对这种情况,所以我更喜欢使用通用的解决方案来简化操作.对我来说,我将使用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="{StaticResource MultiBoolOrConverter}">
<Binding Path="IsChecked" ElementName="enabledOne" />
<Binding Path="IsChecked" ElementName="enabledTwo" />
</MultiBinding>
</TextBox.IsEnabled>
</TextBox>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
和使用的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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
您也可能面临需要使用的情况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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这会对你有所帮助.
更新---------------------
在我以前的解决方案中,我使用了逻辑:
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>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7996 次 |
| 最近记录: |