如何将标签的Visibility属性绑定到许多RadioButtons?

时间:2016-10-06 07:19:46

标签: c# .net wpf xaml data-binding

可以使用BoolToVisiblityConverter将Label的Visibility属性连接到RadioButton的IsChecked属性。但是如何将一个标签的可见性连接到多个IsChecked属性?

例如,当RadioButtons 1,3或5将IsChecked设置为<Label Content="1"></Label>,当检查RadioButtons 2或4时true时,<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>

2 个答案:

答案 0 :(得分:2)

您可以使用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>

答案 1 :(得分:1)

如果您希望LabelRadioButton的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方法中,我们检查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>

不要忘记将转换器的命名空间(转义前缀)映射到:

xmlns:conv="clr-namespace:..."