Label的Visibility-property可以使用BoolToVisiblityConverter连接到RadioButton的IsChecked-property。但是如何将一个标签的可见性连接到多个ischecked属性?
例如,当RadioButtons 1,3或5的IsChecked设置为true
时,<Label Content="1"></Label>
应该可见,当RadioButtons 2或4被选中时,<Label Content="2"></Label>
。
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel>
<RadioButton Content="1"></RadioButton>
<RadioButton Content="2"></RadioButton>
<RadioButton Content="3"></RadioButton>
<RadioButton Content="4"></RadioButton>
<RadioButton Content="5"></RadioButton>
</StackPanel>
<StackPanel Grid.Row="1">
<Label Content="1"></Label>
<Label Content="2"></Label>
</StackPanel>
</Grid>
您可以使用MultiDataTrigger完全在Xaml中设置值,而无需使用转换器:
<StackPanel>
<RadioButton x:Name="RadioButton1" Content="1" />
<RadioButton x:Name="RadioButton2" Content="2" />
<RadioButton x:Name="RadioButton3" Content="3" />
<RadioButton x:Name="RadioButton4" Content="4" />
<RadioButton x:Name="RadioButton5" Content="5" />
</StackPanel>
<StackPanel Grid.Row="1">
<Label x:Name="FirstLabel" Content="1">
<Label.Style>
<Style>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=RadioButton1, Path=IsChecked}" Value="True" />
<Condition Binding="{Binding ElementName=RadioButton3, Path=IsChecked}" Value="True" />
<Condition Binding="{Binding ElementName=RadioButton5, Path=IsChecked}" Value="True" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter TargetName="FirstLabel" Property="Visibility" Value="Visible" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</StackPanel>
当您希望Label
在RadioButton
s 1,3,5检查时可见时,您可以尝试这种方式。定义一个多绑定和一个转换器。转换器:
public class LabelVisibilityConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null) return false;
return values.Any(v =>
{
bool? b = v as bool?;
return b.HasValue && b.Value;
});
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
这是一个例子。在Convert方法中,我们检查from values参数是否为bool且值为true。以下是如何在标记中使用转换器:
<Grid>
<Grid.Resources>
<conv:LabelVisibilityConverter x:Key="LabelConverter"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel>
<RadioButton x:Name="1" Content="1"></RadioButton>
<RadioButton x:Name="2" Content="2"></RadioButton>
<RadioButton x:Name="3" Content="3"></RadioButton>
<RadioButton x:Name="4" Content="4"></RadioButton>
<RadioButton x:Name="5" Content="5"></RadioButton>
</StackPanel>
<StackPanel Grid.Row="1">
<Label Content="1">
<Label.Visibility>
<MultiBinding Converter="{StaticResource LabelConverter}">
<Binding Path="IsChecked" ElementName="1"/>
<Binding Path="IsChecked" ElementName="3"/>
<Binding Path="IsChecked" ElementName="5"/>
</MultiBinding>
</Label.Visibility>
</Label>
<Label Content="2">
<Label.Visibility>
<MultiBinding Converter="{StaticResource LabelConverter}">
<Binding Path="IsChecked" ElementName="2"/>
<Binding Path="IsChecked" ElementName="4"/>
</MultiBinding>
</Label.Visibility>
</Label>
</StackPanel>
</Grid>
不要忘记映射你的转换器的命名空间(conv前缀):
xmlns:conv="clr-namespace:..."