【发布时间】:2017-07-09 04:35:58
【问题描述】:
我是 Wpf 的新手 我有一组 3 个单选按钮 对于所有复选框,当没有选中任何单选按钮或选中组上的第三个单选按钮时,它是不可见的。
我想知道是否有办法实现这一目标? 我尝试内置 booleanToVisibility 但它不起作用。 我需要使用多数据触发器之类的东西吗?谢谢!
【问题讨论】:
标签: wpf binding radio-button converter
我是 Wpf 的新手 我有一组 3 个单选按钮 对于所有复选框,当没有选中任何单选按钮或选中组上的第三个单选按钮时,它是不可见的。
我想知道是否有办法实现这一目标? 我尝试内置 booleanToVisibility 但它不起作用。 我需要使用多数据触发器之类的东西吗?谢谢!
【问题讨论】:
标签: wpf binding radio-button converter
您对 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 以及其他谷歌建议。
【讨论】: