我在SortedList周围创建了一个包装类。我向这个类添加对象,期望它们按字母顺序自动排序,但是当我在WPF中绑定到ListBox时,我会以未排序的顺序看到它。
public class SortedObservableCollection<T> : ICollection<T>, INotifyPropertyChanged, INotifyCollectionChanged where T : INotifyPropertyChanged//, IComparable<T>
{
private readonly SortedList<string,T> _innerSortedList;
public SortedObservableCollection()
{
_innerSortedList = new SortedList<string, T>();
}
public void Add(T item)
{
_innerSortedList.Add(item.ToString(), item);
this.OnPropertyChanged("Count");
this.OnPropertyChanged("Item[]");
this.OnCollectionChanged(NotifyCollectionChangedAction.Add, item);
item.PropertyChanged += ItemPropertyChanged;
}
void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
throw new NotImplementedException();
}
public void Clear()
{
_innerSortedList.Clear();
}
public bool Contains(T item)
{
return _innerSortedList.ContainsKey(item.ToString());
}
public void CopyTo(T[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public int Count
{
get { return _innerSortedList.Count; }
}
public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}
public bool Remove(T item)
{
bool success = _innerSortedList.Remove(item.ToString());
if (success)
{
item.PropertyChanged -= ItemPropertyChanged;
this.OnPropertyChanged("Count");
this.OnPropertyChanged("Item[]");
this.OnCollectionChanged(NotifyCollectionChangedAction.Remove, item);
}
return success;
}
public IEnumerator<T> GetEnumerator()
{
throw new NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _innerSortedList.GetEnumerator();
}
protected virtual void OnPropertyChanged([CallerMemberName] String propertyName = "")
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
protected virtual void OnCollectionChanged(NotifyCollectionChangedAction action, object item)
{
if (this.CollectionChanged != null)
{
this.CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, item));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public event NotifyCollectionChangedEventHandler CollectionChanged;
}
要绑定我只需:
SortedObservableCollection<IRCUser> Users { get; private set; }
.. fill users...
lstUsers.ItemsSource = users;
示例输入:
5Muhammad0
2Muhammad1
5Muhammad2
输出也显示类似,以2开头的那些,4等在5的间隔。
注意:我的IRCUser
类确实实现了IComparable,但由于我现在只想按字母顺序排序,所以我对实现进行了评论,希望默认排序可以启动。
注意2:我已经覆盖了toString()方法,我忘了提及:
public override string ToString()
{
return (int)Type + Nick;
}
更新:
sortedList
内部似乎维持了正确的顺序,但它没有以正确的顺序传递给ListBox
......
答案 0 :(得分:1)
您未正确创建事件对象NotifyCollectionChangedEventArgs。根据操作,此对象具有不同的构造函数重载。在创建新项目时,必须使用使用新项目索引的重载:
new NotifyCollectionChangedEventArgs(action, item, index)
来自MSDN的引用:
初始化NotifyCollectionChangedEventArgs的新实例 描述添加或删除更改的类。
NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction, Object, Int32)
更新0
最好不要使用方法ToString
的重载来比较项目,并为此使用特殊的IComparer<TKey>
。
在您的情况下,正确的代码如下所示:
public void Add(T item)
{
var key = item.ToString();
_innerSortedList.Add(key, item);
this.OnPropertyChanged("Count");
this.OnPropertyChanged("Item[]");
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, _innerSortedList.IndexOfKey(key)));
item.PropertyChanged += ItemPropertyChanged;
}
public bool Remove(T item)
{
var indexOfKey = _innerSortedList.IndexOfKey(item.ToString());
if (indexOfKey == -1)
return false;
_innerSortedList.RemoveAt(indexOfKey);
item.PropertyChanged -= ItemPropertyChanged;
this.OnPropertyChanged("Count");
this.OnPropertyChanged("Item[]");
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item,
indexOfKey));
return true;
}
public IEnumerator<T> GetEnumerator()
{
return _innerSortedList.Values.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
{
var handler = this.CollectionChanged;
if (handler != null)
{
handler(this, args);
}
}
答案 1 :(得分:0)
您正在item.ToString()
对数据进行排序,这可能不是一个非常有用的排序值。
除非被覆盖,否则它将是类的类型名称,因此对于您添加的所有内容都是相同的。
这显然会导致一个问题,如何对通用数据进行排序。这就是IComparable<T>
的用途。
您会注意到有几个SortedList<T>
constructors允许您传递IComparable
实施以定义您自己的订单。
在任何情况下,如果要使用字符串的默认IComparable
实现,则需要使用根据所需顺序不同的字符串。不是完全没有差异的类型名称。
答案 2 :(得分:0)
问题在于
_innerSortedList.Add(item.ToString(), item);
假设项目属于Project.Test.CustomEntity
类型,那么item.ToString()
将为您提供“Project.Test.CustomEntity
”,它将按实体的全名排序,而不是按值排序。
您需要根据T的属性选择器编写自定义排序器。
看看这个article。
答案 3 :(得分:0)
我不确定但是:
1)如果UI有一个数据模板,并且您显示的示例输出不存在IRCUser.ToString()的结果 - 也许,ToString()不会为排序提供“良好”的哈希值
2)使用PagedCollectionView包装你的集合并在那里设置排序可能会更好。