繁体   English   中英

WPF 绑定可见性

[英]WPF binding visibility

我有标签,当 RadioButton FirstSecond IsChecked=true和 Collapsed 当ThirdFourth IsChecked=false时应该可见。 是否只有一种方法可以将按钮的名称传递给转换器并在转换器中决定它应该折叠还是可见?

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Vertical">
        <RadioButton Content="Visible" x:Name="First"></RadioButton>
        <RadioButton Content="Visible" x:Name="Second"></RadioButton>
        <RadioButton Content="Collapsed" x:Name="Third"/>
        <RadioButton Content="Collapsed" x:Name="Fourth"></RadioButton>
    </StackPanel>
    <Label Content="Test" Grid.Row="1"></Label>
</Grid>

您可以实现IMultiValueConverter并使用它来将Visibility绑定到多个值。 你可以在这里找到例子

我建议将 MVVM 模式与MVVM Light 之类的框架一起使用。 然后在视图模型中,您可以拥有标签绑定到的 LabelVisiblity 属性。

如果 MVVM 模式不是一个选项,您可以为每个 CheckBox 的 Checked 事件添加事件处理程序。 然后在那里设置可见性。

    public MainWindow()
    {
        InitializeComponent();

        textBlock.Visibility = Visibility.Collapsed;

        option1.Checked += Option_Checked;
        option2.Checked += Option_Checked;
        option3.Checked += Option_Checked;
        option4.Checked += Option_Checked;
    }

    private void Option_Checked(object sender, RoutedEventArgs e)
    {
        var option = (sender as RadioButton);
        if (option.Name == "option1" || option.Name == "option2")
            textBlock.Visibility = Visibility.Collapsed;
        else
            textBlock.Visibility = Visibility.Visible;

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM