Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ListViewItem item CheckBox. How to get all selected items?

I have a this code:

<ListView Height="238" 
          HorizontalAlignment="Left" 
          Name="listView1" 
          VerticalAlignment="Top" 
          Width="503"
          ItemsSource="{Binding}"
          IsSynchronizedWithCurrentItem="True">
  <ListView.View>
    <GridView>
      <GridView.Columns>
        <GridViewColumn>
          <GridViewColumn.CellTemplate>
            <DataTemplate>
              <CheckBox Tag="{Binding ID}"/>
            </DataTemplate>
          </GridViewColumn.CellTemplate>
        </GridViewColumn>
        <GridViewColumn DisplayMemberBinding="{Binding ID}" Header="ID" />
        <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" />
      </GridView.Columns>
    </GridView>
  </ListView.View>
</ListView>

That produce this window:

Window rendered by code

How do I know how many checkboxes are selected, and get the value Tag of each CheckBox that is selected?

like image 436
Nhu Nguyen Avatar asked Apr 09 '11 15:04

Nhu Nguyen


2 Answers

i know it's old but for posterity if people stubble upon this here's the solution

<ListView Height="238" 
              HorizontalAlignment="Left" 
              Name="listView1" 
              VerticalAlignment="Top" 
              Width="503"
              ItemsSource="{Binding}"
              IsSynchronizedWithCurrentItem="True"
              SelectionChanged="listView1_SelectionChanged">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                               <CheckBox Tag="{Binding ID}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />  
                           </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn DisplayMemberBinding="{Binding ID}" Header="ID" />
                    <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" />
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

then in the cs file code this in the listView1_SelectionChanged

private List<MyObject> lstMyObject = new List<MyObject>();

private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    foreach (MyObject item in e.RemovedItems)
    {
        lstMyObject.Remove(item);
    }

    foreach (MyObject  item in e.AddedItems)
    {
       lstMyObject.Add(item);
    }
}

lstMyObject should be same type as your object binded to the list. and the code will simply add and remove reference to items of the original list to that list.

Now all you will have to do is loop through that list which will contain only the actually selected items. this works for single selection only except that the lstMyObject will just contain 1 record all the time.

like image 152
Franck Avatar answered Oct 29 '22 13:10

Franck


It should be as simple as binding the IsChecked property of the CheckBox to a property on the ViewModel (you may need to add a new property if it doesn't already exist). Then, once the button is clicked, you would just iterate over all the items in the collection, and delete the ones that are checked (based on the value of the property on the ViewModel).

like image 44
jeremyalan Avatar answered Oct 29 '22 13:10

jeremyalan