如何获取选择项目在WPF CheckBox ListBox [英] How to get Selected items in WPF CheckBox ListBox
本文介绍了如何获取选择项目在WPF CheckBox ListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Am使用列表框项中的复选框,如何从列表中获取选中的复选框
< ListBox ItemsSource = {Binding NameList}HorizontalAlignment =LeftMargin =16,68,0,12Name =listBox1Width =156IsEnabled =TrueSelectionMode =MultipleFocusable =TrueIsHitTestVisible =True IsTextSearchEnabled =FalseFontSize =12Padding =5SelectionChanged =listBox1_SelectionChanged>
< ListBox.ItemTemplate>
< DataTemplate>
< StackPanel Orientation =Horizontal>
< CheckBox Content ={Binding Path = CNames}/>
< / StackPanel>
< / DataTemplate>
< /ListBox.ItemTemplate>
< / ListBox>
我尝试循环遍历listboxitems中的选定项目,但它在listboxitem中抛出异常
private void btnSelected(object sender,RoutedEventArgs e)
{
foreach(listBox1.Items中的ListBoxItem项)
{
if(item.ToString()==true)
{
MessageBox.Show(item.Content.ToString());
}
}
}
解决方案>
您可以将每个项目的数据上下文从UI移开,并创建一个对象的ObservableCollection
public ObservedCollection< CheckedItem> List {get; set;}
public class CheckedItem:INotifyPropertyChanged
{
private bool selected;
private string description;
public bool选择
{
get {return selected; }
set
{
selected = value;
OnPropertyChanged(Selected);
}
}
public string描述
{
get {return description; }
set
{
description = value;
OnPropertyChanged(Description);
}
}
/ * INotifyPropertyChanged实现* /
}
然后在您的ListBox ItemTemplate中
< ItemTemplate>
< DataTemplate>
< CheckBox IsChecked ={Binding Path = Selected}
Content = {Binding Path = Description}/>
< / DataTemplate>
< / ItemTemplate> ;
您选择的项目现在可在ObservableCollection中使用,而不是循环使用UI元素
Am Using the checkbox in listbox items, how to get the selected checkboxes from the list
<ListBox ItemsSource="{Binding NameList}" HorizontalAlignment="Left" Margin="16,68,0,12" Name="listBox1" Width="156" IsEnabled="True" SelectionMode="Multiple" Focusable="True" IsHitTestVisible="True" IsTextSearchEnabled="False" FontSize="12" Padding="5" SelectionChanged="listBox1_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding Path=CNames}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I tried to loop thru the selected items in the listboxitems, but it throws exception in listboxitem
private void btnSelected(object sender, RoutedEventArgs e)
{
foreach (ListBoxItem item in listBox1.Items)
{
if (item.ToString() == "true")
{
MessageBox.Show(item.Content.ToString());
}
}
}
解决方案
You could move the data context for each of these items away from the UI and create an ObservableCollection of objects
public ObservableCollection<CheckedItem> List { get;set;}
public class CheckedItem : INotifyPropertyChanged
{
private bool selected;
private string description;
public bool Selected
{
get { return selected; }
set
{
selected = value;
OnPropertyChanged("Selected");
}
}
public string Description
{
get { return description; }
set
{
description = value;
OnPropertyChanged("Description");
}
}
/* INotifyPropertyChanged implementation */
}
Then in your ListBox ItemTemplate
<ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Selected}"
Content={Binding Path=Description}" />
</DataTemplate>
</ItemTemplate>
Your selected items are now available in the ObservableCollection rather than looping through UI elements
这篇关于如何获取选择项目在WPF CheckBox ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文