如何在 WPF 中获取 ListView 的选中行值 [英] How to get Checked rows values of a ListView in WPF
本文介绍了如何在 WPF 中获取 ListView 的选中行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 WPF 应用程序中有一个 ListView
和一个 CheckBox
.
I have a ListView
in WPF application with a CheckBox
.
我想将所有选中的行的值保存在 WPF 列表中...
I want to save the values of all Checked rows in a WPF List ...
我怎样才能做到这一点?
How can I achieve this?
我的列表视图
<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>
推荐答案
您可以将 IsChecked
属性直接绑定到 ListViewItem 的 IsSelected
.使用 RelativeSource
绑定到元素.
You can bind the IsChecked
property directly to IsSelected
of the ListViewItem. Use RelativeSource
to bind to the element.
IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem},Path=IsSelected}"
现在如果你对ListView使用SelectionMode=Multiple
,你可以直接使用SelectedItems
拉取选中的项目.
Now if you use SelectionMode=Multiple
for the ListView, you can pull the checked items directly using SelectedItems
.
var chapters = new List<Chapter>();
foreach (var item in listViewChapter.SelectedItems)
users.Add((Chapter)item);
这篇关于如何在 WPF 中获取 ListView 的选中行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文