如何在 WPF 中获取 ListView 的选中行值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12986543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:51:17  来源:igfitidea点击:

How to get Checked rows values of a ListView in WPF

c#wpfxaml

提问by Avinash Singh

I have a ListViewin WPF application with a CheckBox.

我有一个ListView带有CheckBox.

I want to save the values of all Checked rows in a WPF List ...

我想将所有选中行的值保存在 WPF 列表中...

How can I achieve this?

我怎样才能做到这一点?

My ListView

我的列表视图

<ListView x:Name="listViewChapter" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0" SelectionMode="Single" Height="100" Margin="22,234,17,28" Grid.Row="1">
    <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" >
                    <Label Name="lblChapterID" VerticalAlignment="Center"  Margin="0" Content="{Binding ChapterID}" Visibility="Hidden" />
                    <CheckBox Name="chkChapterTitle" VerticalAlignment="Center" Margin="0,0,0,0" Content="{Binding ChapterTittle}" Checked="chkChapterTitle_Checked" />
                </StackPanel>
            </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

回答by McGarnagle

You can bind the IsCheckedproperty directly to IsSelectedof the ListViewItem. Use RelativeSourceto bind to the element.

您可以将该IsChecked属性直接绑定到IsSelectedListViewItem。使用RelativeSource绑定到元素。

IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem},Path=IsSelected}"

Now if you use SelectionMode=Multiplefor the ListView, you can pull the checked items directly using SelectedItems.

现在,如果您SelectionMode=Multiple用于 ListView,则可以直接使用SelectedItems.

var chapters = new List<Chapter>();
foreach (var item in listViewChapter.SelectedItems)
    users.Add((Chapter)item);

回答by devdigital

You should consider using the MVVM patternfor your WPF application, and if you're going to use MVVM then you'll want an MVVM framework.

您应该考虑为您的 WPF 应用程序使用MVVM 模式如果您打算使用 MVVM,那么您将需要一个 MVVM 框架

Then, it would be a case of creating a type that represents your databound object (e.g Book), then having a collection of this type on your view model (e.g. ObservableCollection<Book> Books).

然后,这将是创建一个代表您的数据绑定对象(例如Book)的类型,然后在您的视图模型(例如ObservableCollection<Book> Books)上拥有这种类型的集合的情况。

You would then bind a Selectedboolean property for example on your Booktype to the CheckBox's IsCheckedproperty in your ListBox ItemTemplate.

然后,您可以将类型Selected上的布尔属性绑定BookIsCheckedListBox ItemTemplate 中的 CheckBox属性。

<CheckBox IsChecked="{Binding Selected}" />

You may not want to polute your domain object (Book) with properties purely used for the UI (Selected), so you could create a BookViewModeltype which augments the Booktype and reshapes the object purely for the purposes of the view.

您可能不想Book使用纯粹用于 UI ( Selected) 的属性来污染域对象 ( ) ,因此您可以创建一个BookViewModel类型来扩充Book类型并纯粹为了视图的目的而重塑对象。