先来看这样演示程序。在修复前,选择重复的数字会导致选中状态混乱,出现多个数字莫名其妙被选中的状况。
刨根问题,此问题最终定位到如下代码(WPF 项目,Silverlight项目类似):
/* internal bool Select(object o, bool assumeInItemsCollection); Declaring Type: System.Windows.Controls.Primitives.Selector+SelectionChanger Assembly: PresentationFramework, Version=4.0.0.0 */ internal bool Select(object o, bool assumeInItemsCollection) { if (!Selector.ItemGetIsSelectable(o)) return false; if (!assumeInItemsCollection && !this._owner.Items.Contains(o)) { if (!this._toDeferSelect.Contains(o)) this._toDeferSelect.Add(o); return false; } if (!this._toUnselect.Remove(o)) { if (this._owner._selectedItems.Contains(o)) return false; if (this._toSelect.Contains(o)) return false; if (!this._owner.CanSelectMultiple && this._toSelect.Count > 0) { foreach (object obj2 in (IEnumerable) this._toSelect) { this._owner.ItemSetIsSelected(obj2, false); } this._toSelect.Clear(); } this._toSelect.Add(o); } return true; }
由于使用的是Items.Contains(o)判断,所以会导致值类型的重复数据判断出错。当然修复此问题的思想就是把值类型转换为引用类型。
创建如下代码
public class WrapObject<T> { private T _obj; public WrapObject(T o) { _obj = o; } public static implicit operator WrapObject<T>(T o) { return new WrapObject<T>(o); } public static explicit operator T(WrapObject<T> o) { return o._obj; } public T UnWrap() { return _obj; } public override string ToString() { return _obj == null ? null : _obj.ToString(); } }
绑定方法修改为
listBox2.ItemsSource = new WrapObject<int>[] { 1, 2, 3, 4, 5, 6, 1, 4, 5, 2, 3, 6 };
此问题到此解决