我想您可以自己在集合中实现 INotifyCollectionChanged 接口,该集合可以侦听 WizardControl 的属性更改事件参数,让您完全控制事情的完成方式。我已经包含了一个如何完成的小样本。
WizardControl.cs
public class WizardControl : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
int zIndex;
PropertyChangedEventArgs zIndexArgs = new PropertyChangedEventArgs("ZIndex");
public int ZIndex
{
get { return zIndex; }
set
{
if (zIndex != value)
{
zIndex = value;
PropertyChangedEventHandler temp = PropertyChanged;
if (temp != null)
temp(this, zIndexArgs);
}
}
}
public override string ToString()
{
return zIndex.ToString();
}
}
WizardCollection.cs
public class WizardCollection : INotifyCollectionChanged, IEnumerable<WizardControl>
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
NotifyCollectionChangedEventArgs collectionChangedMoveArgs = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
List<WizardControl> items = new List<WizardControl>();
public WizardControl this[int index]
{
get { return items[index]; }
}
public void Add(WizardControl item)
{
if (items == null) items = new List<WizardControl>();
items.Add(item);
item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
NotifyCollectionChangedEventHandler temp = CollectionChanged;
if (temp != null)
temp(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
}
public void Remove(WizardControl item)
{
item.PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
NotifyCollectionChangedEventHandler temp = CollectionChanged;
if (temp != null)
temp(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
}
void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "ZIndex")
{
items = items.OrderBy(x => x.ZIndex).ToList();
NotifyCollectionChangedEventHandler temp = CollectionChanged;
if (temp != null)
temp(this, collectionChangedMoveArgs);
}
}
public IEnumerator<WizardControl> GetEnumerator()
{
return items.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return items.GetEnumerator();
}
}