【问题标题】:How do I bind combobox IsChecked to 4 buttons from the same group?如何将组合框 IsChecked 绑定到同一组中的 4 个按钮?
【发布时间】:2018-04-28 08:59:09
【问题描述】:

我希望在按下其中一个单选按钮时启用组合框。

<RadioButton x:Name="A" GroupName="rButton" Content="A" Grid.Column="4"/>
<RadioButton x:Name="B" GroupName="rButton" Content="B" Grid.Column="4"/>
<RadioButton x:Name="C" GroupName="rButton" Content="C" Grid.Column="4"/>
<RadioButton x:Name="D" GroupName="rButton" Content="D" Grid.Column="4"/>

<ComboBox IsEnabled="{Binding IsChecked,?? }" Grid.Column="5" Width="120" Height="30"/>

【问题讨论】:

  • 澄清一下,您希望在选中任何个 RadioButtons 后启用 ComboBox?
  • @Manfred Radlwimmer 是的

标签: c# wpf combobox radio-button


【解决方案1】:

如果您想通过 Bindings 解决这个问题(并且您应该),您需要一个返回 trueMultiBindingConverter,只要其中一个值为 true(布尔 OR):

public class BooleanOrConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        foreach (object value in values)
        {
            if (value is bool && (bool) value)
                return true;
        }
        return false;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return Enumerable.Repeat(DependencyProperty.UnsetValue, targetTypes.Length).ToArray();
    }
}

定义:

<Window.Resources>
    <local:BooleanOrConverter x:Key="OrConverter"/>
</Window.Resources>

用法:

<RadioButton x:Name="RadioButtonSource" GroupName="rButton" Content="A" Grid.Column="4"/>
<RadioButton x:Name="RadioButtonToken" GroupName="rButton" Content="B" Grid.Column="4"/>
<RadioButton x:Name="RadioButtonII" GroupName="rButton" Content="C" Grid.Column="4"/>
<RadioButton x:Name="RadioButtonUkey" GroupName="rButton" Content="D" Grid.Column="4"/>

<ComboBox Grid.Column="5" Width="120" Height="30">
    <ComboBox.IsEnabled>
        <MultiBinding Converter="{StaticResource OrConverter}">
            <Binding ElementName="RadioButtonSource" Path="IsChecked"/>
            <Binding ElementName="RadioButtonToken" Path="IsChecked"/>
            <Binding ElementName="RadioButtonII" Path="IsChecked"/>
            <Binding ElementName="RadioButtonUkey" Path="IsChecked"/>
        </MultiBinding>
    </ComboBox.IsEnabled>
</ComboBox>

这样,只要RadioButtonsIsChecked 属性中的任何 变为trueComboBox 就会启用。如果你重置RadioButtons,它会再次被禁用。

【讨论】:

  • 如果只是视觉操作,为什么要使用viewmodel呢?在我看来,UI 的东西应该在后面的代码中耗尽。如果不仅仅是为了使可以通过点击事件完成的简单任务复杂化,那么没有理由通过视图模型
  • @DanieleSartori 我不知道你在说什么,我的代码中绝对没有 ViewModel。至于“在我看来,UI 的东西应该在代码后面用尽”我完全不同意这种说法。你会在代码隐藏中生成VisualStatesStoryBoardsDataTemplatesStyles 吗?我希望不会。
  • @DanieleSartori 我不同意你的看法。 UI 内容应该在 XAML 或 ResourceDictionaries 中。
  • 我的错,我看错了你的代码。然而,我永远不会梦想在代码后面做 VisualStates、StoryBoards、DataTemplates 和 Styles。这应该在 xaml 中完成。我指的是这种操作(即,如果这与 UI 上的另一个操作相关,则启用或禁用控件)
  • @DanieleSartori 实际上,最好的方法是使用 ViewModel,但 OPs 代码中没有,所以我没有介绍。顺便说一句:(关于您的回答) RadioButton_Click 将如何导致未选中的 RadioButton?我认为您可以删除代码的最后两行。
猜你喜欢
  • 1970-01-01
  • 2016-08-23
  • 1970-01-01
  • 2012-07-24
  • 1970-01-01
  • 2015-10-19
  • 2021-05-24
  • 2012-11-12
  • 1970-01-01
相关资源
最近更新 更多