1
Vote

Databound ListView with CheckBoxes and handling IsChecked property in WPF.

description

Hi everyone!
 
I'm trying to develop something like this...
 
I have WPF application with user logging enabled. And when creating a user for the system; the Admin have to choose what are the departments that the new user could work for. So there is a ListView that showing available departments to choose (by checking the relevant checkboxes of departments). And the selection of departments should be easy (when selecting a row, its CheckBox should be checked. And it's now OK.)
 
My problems are;
 
  1. How to get IsChecked or IsSelected items (CheckBoxes) in the ListView through a loop.
  2. When an existing user is selected; I want to display all the departments on the ListView with previously selected departments are already selected.
     
    Please help me at this. This is the ListView.
 
<ListView ItemsSource="{Binding}" Name="lvDepartments" SelectionMode="Multiple">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox VerticalAlignment="Center"
IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}">
<CheckBox.Content>
<TextBlock Text="{Binding DepID}" Width="0"/>
</CheckBox.Content>
</CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding DepID}" Header="DepartmentID" Width="0" >
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Department" Width="340" />
</GridView>
</ListView.View>
</ListView>

comments