绑定到Queue< string>.用户界面永远不会更新 [英] Binding to Queue<string>. UI never updates

查看:51
本文介绍了绑定到Queue< string>.用户界面永远不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 ListBox 绑定到 Queue< string> .当我使项目入队/出队时, ListBox 不会更新.

I bound a ListBox to a Queue<string>. When I enqueue/dequeue items, the ListBox does not update.

我有帮忙入队/出队的助手,以进行财产变更

I have helpers for enqueue/dequeue to raise property change

protected void EnqueueWork(string param)
{
    Queue.Enqueue(param);
    RaisePropertyChanged("Queue");
}

protected string DequeueWork()
{
    string tmp = Queue.Dequeue();
    RaisePropertyChanged("Queue");
    return tmp;
} 

推荐答案

您实现了 INotifyCollectionChanged 吗?您需要使用它来通知操作,例如在集合中添加或删除项目.

Have you implemented INotifyCollectionChanged ? you need this for notifications of actions like adding or removing items from a collection.

这是队列的简单实现:

public class ObservableQueue<T> : INotifyCollectionChanged, IEnumerable<T>
{
    public event NotifyCollectionChangedEventHandler CollectionChanged;
    private readonly Queue<T> queue = new Queue<T>();

    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<T> GetEnumerator()
    {
        return queue.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

这篇关于绑定到Queue&lt; string&gt;.用户界面永远不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆