本文介绍了从wpf复选框列表中获取所有选定的项目给我错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的列表框带有复选框,如下所示,&它从sql server数据库绑定它的数据, ,我想得到选定的项值当我运行这个但是我收到了这个错误 无法将'System.Data.DataRowView'类型的对象强制转换为'System.Windows.Controls.CheckBox'。 < Window.Resources> < DataTemplate x:Key = NameColumnTemplate > < CheckBox Height = 20 FontFamily = Arial FontSize = 14 Content = {Binding Path = PermissionDescription} Tag = {Binding PermissionID} HorizontalAlignment = 拉伸 VerticalAlignment = 中心 /> < / DataTemplate > < / Window.Resources > < Grid> < ListBox HorizontalAlignment = 拉伸 Margin = 12,12,136,21名称= lstEmployees VerticalAlignment = Stretch ItemsSource = {Binding Tables [0]} ItemTemplate = {StaticResource NameColumnTemplate} ScrollViewer.VerticalScrollBarVisibility = Auto removed = {x:Null} BorderBrush = #FF AD7F30 SelectionChanged = lst_SelectionChanged CheckBox.Click = lst_SelectionChanged /> < Button Content = listbox高度= 23 HorizontalAlignment = 左 Margin = 214,207,0,0 Name = btnShowSelectedItems VerticalAlignment = Top Width = 75 Click = btnShowSelectedItems_Click /> < / 网格 > public Window2( ) { InitializeComponent(); // 绑定数据 lstEmployees.DataContext = SelJobsCat(); } private void btnShowSelectedItems_Click( object sender,RoutedEventArgs e) { foreach (CheckBox item in lstEmployees.Items) { if (item.IsChecked == true ) { System.Windows.MessageBox.Show((item.Content + 被选中。)); } } } private void lst_SelectionChanged( object sender,RoutedEventArgs e) { if (e.OriginalSource CheckBox) { lstEmployees.SelectedItem = e.OriginalSource; } if (lstEmployees.SelectedItem == null ) 返回; Console.WriteLine(lstEmployees.SelectedIndex); Console.WriteLine(((CheckBox)lstEmployees.SelectedItem).IsChecked); } 我的错误在哪里,谢谢解决方案 使用CheckBoxList的GetItemChecked或GetItemCheckState方法 for(int i = 0; i< lstEmployees.Items.Count ; i ++) if(clbIncludes.GetItemChecked(i)) System.Windows.MessageBox.Show((item.Content +is checked。));; else //未选择的东西 或者如果你只想包括实际检查的项目: if(stEmployees.GetItemCheckState(i)= = CheckState.Checked) 或listitem.selected: foreach(lstEmployees.Items中的ListItem listItem) { if(listItem.Selected) { //做一些工作} else { //做一些工作} } I have listbox with checked box as following, & it binding its data from sql server database,, I want to get selected items value When I run this but I got this errorUnable to cast object of type 'System.Data.DataRowView' to type 'System.Windows.Controls.CheckBox'.<Window.Resources> <DataTemplate x:Key="NameColumnTemplate"> <CheckBox Height="20" FontFamily="Arial" FontSize="14" Content="{Binding Path=PermissionDescription}" Tag="{Binding PermissionID}" HorizontalAlignment="Stretch" VerticalAlignment="Center" /> </DataTemplate> </Window.Resources><Grid><ListBox HorizontalAlignment="Stretch" Margin="12,12,136,21" Name="lstEmployees" VerticalAlignment="Stretch" ItemsSource="{Binding Tables[0]}" ItemTemplate="{StaticResource NameColumnTemplate}" ScrollViewer.VerticalScrollBarVisibility="Auto" removed="{x:Null}" BorderBrush="#FFAD7F30" SelectionChanged="lst_SelectionChanged" CheckBox.Click="lst_SelectionChanged"/><Button Content="listbox" Height="23" HorizontalAlignment="Left" Margin="214,207,0,0" Name="btnShowSelectedItems" VerticalAlignment="Top" Width="75" Click="btnShowSelectedItems_Click" /></Grid>public Window2() { InitializeComponent(); // bind data lstEmployees.DataContext = SelJobsCat(); }private void btnShowSelectedItems_Click(object sender, RoutedEventArgs e) { foreach (CheckBox item in lstEmployees.Items) { if (item.IsChecked == true) { System.Windows.MessageBox.Show((item.Content + " is checked.")); } } } private void lst_SelectionChanged(object sender, RoutedEventArgs e) { if (e.OriginalSource is CheckBox) { lstEmployees.SelectedItem = e.OriginalSource; } if (lstEmployees.SelectedItem == null) return; Console.WriteLine(lstEmployees.SelectedIndex); Console.WriteLine(((CheckBox)lstEmployees.SelectedItem).IsChecked); }where is my error please, Thanks 解决方案 Use the CheckBoxList's GetItemChecked or GetItemCheckState method for (int i = 0; i < lstEmployees.Items.Count; i++) if (clbIncludes.GetItemChecked(i)) System.Windows.MessageBox.Show((item.Content + " is checked.")); else // Do unselected stuffor if you want to only include actually checked items:if (stEmployees.GetItemCheckState(i) == CheckState.Checked)or listitem.selected:foreach (ListItem listItem in lstEmployees.Items){ if (listItem.Selected) { //do some work } else { //do some work }} 这篇关于从wpf复选框列表中获取所有选定的项目给我错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-21 20:26