wpf - checkbox.isvisible 当单选按钮之一被选中或未选中单选按钮时为真

wpf - checkbox.isvisible is ture when one of the radio button is checked or no radio button checked

我是 Wpf 新手 我有一组 3 个单选按钮 对于所有复选框,当单选按钮的 none 被选中或组中的第三个单选按钮被选中时,它是不可见的。

请问有没有办法实现? 我尝试内置 booleanToVisibility 但它不起作用。 我需要使用多数据触发器之类的东西吗?谢谢!

关于 MultiBinding,你是对的。您的 Xaml 应如下所示:

<Window.Resources>
    <local:MultiBoolToVisibilityConverter x:Key="MultiBoolToVisibilityConverter"/>
</Window.Resources>
<DockPanel>
    <StackPanel DockPanel.Dock="Top">
        <RadioButton Name="rb1" Content="1"/>
        <RadioButton Name="rb2" Content="2"/>
        <RadioButton Name="rb3" Content="3"/>
    </StackPanel>
    <CheckBox DockPanel.Dock="Bottom" Content="Visible when 1 or 2 is checked.">
        <CheckBox.Visibility>
            <MultiBinding Converter="{StaticResource MultiBoolToVisibilityConverter}">
                <Binding Path="IsChecked"  ElementName="rb1" />
                <Binding Path="IsChecked"  ElementName="rb2" />
                <Binding Path="IsChecked"  ElementName="rb3" />
            </MultiBinding>
        </CheckBox.Visibility>
    </CheckBox>
</DockPanel>

转换器中的MultiBoolToVisibilityConverter应该在后面的代码中定义,实现IMultiValueConverter

public class MultiBoolToVisibilityConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        bool? firstRadioButtonIsChecked = values[0] as bool?;
        bool? secondRadioButtonIsChecked = values[1] as bool?;
        bool? thirdRadioButtonIsChecked = values[2] as bool?;

        //set your logic. this is just an example:
        if (firstRadioButtonIsChecked == true || secondRadioButtonIsChecked == true)
            return Visibility.Visible;
        return Visibility.Collapsed;

    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

如需更多问题,您可以查看 this post on MultiBinding and IMultiValueConverter 以及其他 google 个建议。