在 XAML 中使用 RadioButtons 启用/禁用 WPF 控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22811923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:26:26  来源:igfitidea点击:

Enable/disable WPF controls using RadioButtons in XAML

c#wpfxamlradio-button

提问by FishySwede

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

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

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

如果选择了多个RadioButton之一,我可以禁用控件,这一点很重要。

E.g.

例如

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

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

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

Note

笔记

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.

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

回答by Amer

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:

如果这是你唯一需要它的地方,那么使用你的简单解决方案是可以的,但我认为在实际应用中你可能会多次遇到这种情况,所以我更喜欢使用通用解决方案来使事情变得更简单。对我来说,我将使用 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>

and the used MultiBoolOrConverter.csconverter class:

和使用的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();
        }
    }
}

you also may face a situation where you need to use ANDinstead of OR, here is the MultiBoolAndConverterconverter:

您还可能面临需要使用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();
        }
    }
}

I wish this will help you.

我希望这会帮助你。

UPDATE ---------------------

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

In my previous solution, I have used the logic:

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

The control should be enabled if one of the first two buttons are selected.

The control should be enabled if one of the first two buttons are selected.

Instead of what you have asked literally:

而不是你从字面上问的:

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

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

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 IsCheckedboolean value:

它当然可以工作,但是如果您完全按照您的描述想要它,那么代码几乎不需要更改即可使用转换器来否定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:

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

回答by FishySwede

The solution I came up with is:

我想出的解决方案是:

<Window.Resources>
    <Style TargetType="TextBox" x:Key="enableDisableStyle">
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsChecked, ElementName=disabledOne}" Value="True">
                <Setter Property="IsEnabled" Value="False" />
            </DataTrigger>
            <DataTrigger Binding="{Binding IsChecked, ElementName=disabledTwo}" Value="True">
                <Setter Property="IsEnabled" Value="False" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</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" Style="{StaticResource enableDisableStyle}" />
</StackPanel>

I will not mark my own answer as accepted until I'm convinced it is the best solution.

在我确信这是最佳解决方案之前,我不会将我自己的答案标记为已接受。