Nhu Nguyen
Nhu Nguyen

Reputation: 874

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?

Upvotes: 15

Views: 45549

Answers (3)

user3923143
user3923143

Reputation: 1

A suggestion...

Just like the Tag property in all Windows controls, I've always had a Tag property in all my data models for general purpose use at run-time. I use that property to hold checked state of the item in a ListView. In other circumstance, I use them to hold complex objects as well.

Upvotes: 0

Franck
Franck

Reputation: 4440

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.

Upvotes: 24

jeremyalan
jeremyalan

Reputation: 4786

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).

Upvotes: 7

Related Questions