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

C# 如何在WPF中获取ListView的选中行值,c#,wpf,xaml,C#,Wpf,Xaml,我在WPF应用程序中有一个带有复选框的列表视图 我想保存WPF列表中所有选中行的值 我怎样才能做到这一点 我的列表视图 <ListView x:Name="listViewChapter" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0" SelectionMode="Single" Height="100" Margin="22,234,17,28" Grid.Row="1">

我在WPF应用程序中有一个带有
复选框的
列表视图

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

我怎样才能做到这一点

我的列表视图

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


> p>您应该考虑使用您的WPF应用程序,和

然后,创建一个表示数据绑定对象的类型(例如
Book
),然后在视图模型上拥有该类型的集合(例如
observedcollection Books

然后,您可以将
选定的
布尔属性绑定到ListBox ItemTemplate中复选框的
IsChecked
属性,例如,在
书籍
类型中

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


您可能不想将域对象(
Book
)的属性纯粹用于UI(
所选
),因此,您可以创建一个
BookViewModel
类型,该类型扩展了
Book
类型,并完全为了视图的目的而重塑对象。

您可以将
IsChecked
属性直接绑定到ListViewItem的
IsSelected
。使用
RelativeSource
绑定到元素

IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem},Path=IsSelected}"
现在,如果对列表视图使用
SelectionMode=Multiple
,则可以使用
SelectedItems
直接提取选中的项目

var chapters = new List<Chapter>();
foreach (var item in listViewChapter.SelectedItems)
    users.Add((Chapter)item);
var chapters=新列表();
foreach(listViewChapter.SelectedItems中的var项)
用户。添加((章)项);

我在ListView中添加了IsChecked=“{Binding RelativeSource={RelativeSource AncestorType=ListViewItem},Path=IsSelected}”。。现在我如何获得选中行值..在复选框中添加此复选框后,仅选中单个复选框,我希望选中多个复选框。。。。