【发布时间】:2015-06-13 15:39:35
【问题描述】:
我正在测试 Kelly Elias 在 Creating a WPF checkListBox 上的文章。我需要获取 selectedIndex 和复选框文本。一切都按需要工作,直到我将列表框的 SelectionMode 更改为我需要实现的“Multiple”。之后,SelectedIndex 和 SelectedItem 都不会使用 SelectionChanged 事件进行更改。这两个属性只显示第一个复选框的信息。但是,所有复选框都添加到 SelectedItems 集合中。有人可以帮忙解决这个问题吗?
提前谢谢你!!!
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace Jarloo
{
public class Customer : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
NotifyPropertyChanged("Name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class CheckedListItem<T> : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool isChecked;
private T item;
public CheckedListItem()
{
}
public CheckedListItem(T item, bool isChecked = false)
{
this.item = item;
this.isChecked = isChecked;
}
public T Item
{
get { return item; }
set
{
item = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Item"));
}
}
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
}
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ObservableCollection<CheckedListItem<Customer>> Customers { get; set; }
public MainWindow()
{
InitializeComponent();
Customers = new ObservableCollection<CheckedListItem<Customer>>();
Customers.Add(new CheckedListItem<Customer>(new Customer() { Name = "Kelly Smith" }));
Customers.Add(new CheckedListItem<Customer>(new Customer() { Name = "Joe Brown" }));
Customers.Add(new CheckedListItem<Customer>(new Customer() { Name = "Herb Dean" }));
Customers.Add(new CheckedListItem<Customer>(new Customer() { Name = "John Paul" }));
DataContext = this;
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = listbox1.SelectedIndex;
string testName = ((CheckedListItem<Customer>)listbox1.SelectedValue).Item.Name;
}
}
}
<Window x:Class="Jarloo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen">
<Grid>
<ListBox Name="listbox1" ItemsSource="{Binding Customers}" SelectionChanged="ListBox_SelectionChanged"
SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"
Content="{Binding Path=Item.Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
【问题讨论】:
-
鉴于 SelectedIndex 是 Multiple 中的第一个,我怀疑这就是它的工作原理。由于您有一个有效的 SelectedItems 集合,请尝试沿着该路径前进。
-
我可以执行以下操作,但我仍然需要 SelectedIndex 进行相应更改: string test = ((CheckedListItem
)listbox1.SelectedItems[listbox1.SelectedItems.Count - 1]).Item.Name ;甚至 CurrentItem 属性也无法正常工作。它卡在列表框字符串 currentItem = ((CheckedListItem 中的第一个项目上)listbox1.Items.CurrentItem).Item.Name; -
需要和获取不一样。如果它不开火,它就不会开火。
-
我不确定你在这里得到了什么。我需要帮助才能使用“Multiple”SelectionMode 更改 SelectedIndex。
-
如果 SelectedIndex 没有随着 Multiple SelectionMode 发生变化,那么您可能无能为力。这就是控制的工作方式。跳出框框寻找其他到达那里的方法。
标签: c# wpf checkedlistbox