复选框列表视图Wpf

时间:2015-06-09 20:59:41

标签: c# wpf listview checkbox

如何为Listview中的列创建复选框。我能够为listview项目制作复选框。但我希望有一个Column本身的复选框。就像在windows中一样:

enter image description here

这是XAML中的代码

    <ListView HorizontalAlignment="Left" Grid.Row="1" Width="400">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="140" Header="Column1 With Checkbox">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Tag="{Binding}" IsThreeState="False" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Width="140" Header="Column2" />
                <GridViewColumn Width="115" Header="Column3" />
            </GridView>
        </ListView.View>
    </ListView>

请注意,这只会使项目的复选框不是列本身。那么如何实现呢?

1 个答案:

答案 0 :(得分:1)

您可以通过在GridViewColumn.Header属性下定义标题来使标头成为自定义控件。

<ListView HorizontalAlignment="Left" Grid.Row="1" Width="400">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="140">
                <GridViewColumn.Header>
                    <StackPanel Orientation="Horizontal">
                        <Checkbox IsChecked="{Binding YourCheckedProperty}" />
                        <TextBlock Text="Column1" />
                    </StackPanel>
                <GridViewColumn.Header>
            </GridViewColumn>

            <GridViewColumn Width="140" Header="Column2" />
            <GridViewColumn Width="115" Header="Column3" />
        </GridView>
    </ListView.View>
</ListView>