隐藏/显示DataTemplate

时间:2012-02-20 09:35:47

标签: c# .net wpf xaml wpf-controls

我在ViewModel中有类Person的数组,我想在表中显示它们的名字。我还有复选框列。这是我的观看部分:

 <ListView ItemsSource="{Binding Persons}">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="40">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox 
                            .........................
                            .... some logic here .... 
                            ......................./>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="140" 
                            Header="Name" 
                            DisplayMemberBinding="{Binding Path=Name}" />
        </GridView>
    </ListView.View>
</ListView>

如何根据IsSelectionAllowed布尔变量的值显示/隐藏复选框列。

1 个答案:

答案 0 :(得分:1)

使用样式:

    <Style x:Key="CheckBoxStyle" TargetType="{x:Type Control}">
        <Setter Property="Visibility" Value="Visible"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsSelectionAllowed}" Value="False">
                <Setter Property="Visibility" Value="Hidden"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

附上:

<CheckBox  Style="{StaticResource CheckBoxStyle}"
                        .........................
                        .... some logic here .... 
                        ......................./>