绑定到队列。 UI永远不会更新
我将ListBox
绑定到Queue
。 当我对项目进行入队/出列时, ListBox
不会更新。
我有帮助入队/出队以提高财产变化
protected void EnqueueWork(string param) { Queue.Enqueue(param); RaisePropertyChanged("Queue"); } protected string DequeueWork() { string tmp = Queue.Dequeue(); RaisePropertyChanged("Queue"); return tmp; }
你有没有实现INotifyCollectionChanged
? 您需要此function来通知添加或删除集合中的项目等操作。
这是队列的简单实现:
public class ObservableQueue : INotifyCollectionChanged, IEnumerable { public event NotifyCollectionChangedEventHandler CollectionChanged; private readonly Queue queue = new Queue (); public void Enqueue(T item) { queue.Enqueue(item); if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Add, item)); } public T Dequeue() { var item = queue.Dequeue(); if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Remove, item)); return item; } public IEnumerator GetEnumerator() { return queue.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }
您应该使用ObservableCollection而不是队列来执行您想要的操作,以允许ListBox更新项目添加和删除您的类应该实现INotifyCollectionChanged,ObservableCollection实现该接口,或者您可以编写实现INotifyCollectionChanged接口的自定义队列(ObservableQueue)
这篇文章可以帮助你
- 如何仅在Entity Framework 6.1中加载子对象的某些字段?
- 是否存在链式NULL检查?
- C#当我准备好程序结束时,如何停止tcpClient.Connect()进程? 它就在那里坐了10秒钟!
- OnMouseMove不会在WPF中的canvas上触发
- c#chart垂直文本注释
- 使用Linq进行分段的XML字符串解析
- SQL Server比较具有相同列的两个表行并返回更改的列
- 为什么Window.FindName()没有发现x:子UserCStrol中按钮的名称? AKA NameScopes如何运作?
- 什么.NET StringComparer等同于SQL的Latin1_General_CI_AS