我是Wpf的新手 我有一组3个单选按钮 对于所有复选框,当没有单选按钮或组中的第三个单选按钮被选中时,它不可见。
我想知道是否有办法实现这一目标? 我尝试内置booleanToVisibility但它不起作用。 我需要使用多个数据触发器吗?谢谢!
答案 0 :(得分:1)
你对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。