jeudi 10 novembre 2016

c# wpf ListView with checkbox - check boxes when loading a form by comparing two lists

I have a form in which I create instances of a class Box. This class Box contains a list of another class that is visualized by a ListView in the form and each item in the ListView can be checked via a checkbox. All checked items in the ListView are saved as a new list as part of the properties of class Box. Now when I want to edit an instances of my class Box I want the form to open again but with the ListView loaded such that all items that where checked before are already checked.

I googled a lot but I was not able to find a solution. Basically I have the list of previously selected items in the ListView and I have the list that is displayed in ListView. Then I have to compare both lists when loading the form and check the checkboxes of entries that are in both lists.

This is my ListView in XAML

<ListView x:Name="PanelListView" Grid.ColumnSpan="3" Grid.Column="1" HorizontalAlignment="Left" Height="85" Margin="0,23,0,0" Grid.Row="4" Grid.RowSpan="3" VerticalAlignment="Top" Width="189" SelectionChanged="PanelListView_SelectionChanged">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="40">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                        <GridViewColumnHeader Tag="select" Content="" />
                    </GridViewColumn>
                    <GridViewColumn Header="Panel Name" Width="140" DisplayMemberBinding="{Binding Name}" />
                </GridView>
            </ListView.View>
</ListView>




Aucun commentaire:

Enregistrer un commentaire