Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
} 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
300 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

here is a simple implementation for queue:

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();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...