在Checked = true模式下将所有复选框添加到Listview

时间:2014-03-26 14:10:17

标签: wpf listview checkbox

我有以下列表视图与复选框,我希望它们被添加复选框被选中。

<ListView Name="FinalListView"
                  SelectionMode="Multiple"
                  SelectionChanged="FinalListView_SelectionChanged"
                  IsSelected="True">

            <ListView.View >
                <GridView>
                    <GridViewColumn Width="30">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Tag="{Binding ID}"
                                          IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" Width="400"/>
                </GridView>
            </ListView.View>
        </ListView>

为什么在将它们添加到列表中时不会显示所有CHECKED。当我的列表视图属性IsSelected="True"和复选框绑定为

<CheckBox Tag="{Binding ID}"  IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />

1 个答案:

答案 0 :(得分:0)

试试这个

<ListView Name="FinalListView" SelectionMode="Multiple" IsSelected="True">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="30">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox Name="checkbox" IsChecked="True"  Tag="{Binding ID}" Content="ok" >
                            <CheckBox.Style>
                                <Style TargetType="CheckBox">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}" Value="True">
                                            <Setter  Property="IsChecked" Value="True"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </CheckBox.Style>
                        </CheckBox>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn  Header="Name" Width="400"/>
        </GridView>
    </ListView.View>       
</ListView>