我有一个ListViewWPF应用程序CheckBox.
我想保存WPF列表中所有Checked行的值...
我怎样才能做到这一点?
我的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>
Run Code Online (Sandbox Code Playgroud)
您可以IsChecked直接将属性绑定到IsSelectedListViewItem.使用RelativeSource绑定到的元素.
IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem},Path=IsSelected}"
Run Code Online (Sandbox Code Playgroud)
现在,如果您使用SelectionMode=MultipleListView,则可以直接使用拉取选中的项目SelectedItems.
var chapters = new List<Chapter>();
foreach (var item in listViewChapter.SelectedItems)
users.Add((Chapter)item);
Run Code Online (Sandbox Code Playgroud)