gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:17:37 28 4
gpt4 key购买 nike

我在 WPF 应用程序中有一个 ListView 和一个 CheckBox

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

我怎样才能做到这一点?

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

最佳答案

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

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

现在,如果您对 ListView 使用 SelectionMode=Multiple,则可以使用 SelectedItems 直接拉取选中的项目。

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

关于c# - 如何在 WPF 中获取 ListView 的选中行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12986543/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com