在 ListView 中使用 MVVM 选中复选框时显示标签,取消选中复选框时隐藏所选行中的标签 - wpf

Using MVVM in ListView on checking a checkbox show a label and on unchecking checkbox hide a label in the selected row - wpf

在我的 wpf 应用程序中,我使用的是 MVVM。在选定行的 ListView 中,我想在同一选定行的复选框的 checking/unchecking 上显示/隐藏标签。

您应该使用内置的 BooleanToVisibilityConverter,并将其绑定到 Checkbox 的 IsChecked 属性。请参阅下面的简单示例。

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/    presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVis"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <CheckBox x:Name="myCheckBox" Content="CheckBox"     VerticalAlignment="Center" HorizontalAlignment="Center"><    /CheckBox>
        <TextBlock Text="Label" Grid.Row="1"      VerticalAlignment="Center" HorizontalAlignment="Center"
            Visibility="{Binding IsChecked,    ElementName=myCheckBox, Converter={StaticResource     BoolToVis}}"></TextBlock>
    </Grid>
</Window>