开发者

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

I have开发者_C百科 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:

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

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


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜