在 XAML 中使用 RadioButtons 启用/禁用 WPF 控件 [英] Enable/disable WPF controls using RadioButtons in XAML

查看:63
本文介绍了在 XAML 中使用 RadioButtons 启用/禁用 WPF 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我有一组 RadioButton,我想根据选择的 RadioButton 禁用另一个控件.

My problem is that I have a set of RadioButtons and I want to disable another control depending on which RadioButton is selected.

如果选择了几个 RadioButton 之一,我可以禁用控件.

It is important that I can disable the control if of one of several RadioButton is selected.

例如

[] Enabled one
[] Enabled two
[] Disabled one
[] Disabled two

如果选择了最后两个按钮之一,则应禁用该控件.

The control should be disabled if one of the last two buttons are selected.

注意

我打算自己回答这个问题(根据此链接应该没问题) 但如果他们以更好的方式解决问题,我会接受其他答案.

I intend to answer this question myself (Which should be ok according to this link) but I will accept other answers if they solve the problem in a better way.

推荐答案

如果这是您唯一需要它的地方,那么使用您的简单解决方案是可以的,但我认为在实际应用中您可能会遇到这种情况情况很多次,所以我更喜欢使用通用解决方案来简化事情.对我来说,我将使用 MultiConverter 使用逻辑 OR 从其他几个布尔值计算结果布尔值,如下所示:

If this is the only place where you need it, then it is ok to use your simple solution, but I think in a real application you will probably face this situation many times, so I prefer to use a general solution to make things simpler. For me, I will use a MultiConverter to calculate a result boolean from several other booleans using logical OR, like this:

<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 转换器:

you also may face a situation where you need to use AND instead of OR , here is the MultiBoolAndConverter converter:

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

希望对您有所帮助.

更新 ---------------------

在我之前的解决方案中,我使用了逻辑:

In my previous solution, I have used the logic:

如果选择了前两个按钮之一,则应启用控件.

而不是你从字面上问的:

Instead of what you have asked literally:

如果选择了最后两个按钮之一,则应禁用该控件.

它当然可以工作,但是如果您完全按照您的描述想要它,那么代码几乎不需要更改即可使用转换器来否定 IsChecked 布尔值:

It is working of course, but if you want it exactly as you’ve described then the code will need little change to use a converter to negate IsChecked boolean value:

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

这篇关于在 XAML 中使用 RadioButtons 启用/禁用 WPF 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆