1

我有一个模型集合,它从我的大部分代码中引用。

public class TraceEntryQueue
    {
        private readonly Queue<TraceEntry> _logEntries; 

        public TraceEntryQueue()
        {
            _logEntries = new Queue<TraceEntry>();
        }

        public void AddEntry(TraceEntry newEntry)
        {
            _logEntries.Enqueue(newEntry);

        }

        public List<TraceEntry> GetAsList()
        {
            return _logEntries.ToList();
        }
    }

这个集合是在我的一个观点中呈现的。

public class LoggingViewModel : ViewModelBase
    {
        private ICollectionView _customers;
        public ICollectionView Customers
        {
            get
            {
                return _customers;
            }
            private set
            {
                _customers = value;

            }
        }

        public LoggingViewModel(TraceEntryQueue traceQueue)
        {

            Customers = CollectionViewSource.GetDefaultView(traceQueue.GetAsList());
            Customers.SortDescriptions.Add(new SortDescription("Index", ListSortDirection.Descending));
            Customers.Refresh();
        }
    }

问题是当我通过添加新条目时如何通知我的视图重新加载集合

            public void AddEntry(TraceEntry newEntry)
            {
                _logEntries.Enqueue(newEntry);

            }
4

3 回答 3

1

使用可观察的集合而不是队列。这将在集合更新时自动通知您的视图(添加/删除等)

public class TraceEntryQueue
{
    private readonly ObservableCollection<TraceEntry> _logEntries; 

    public TraceEntryQueue()
    {
        _logEntries = new ObservableCollection<TraceEntry>();
    }

    public void AddEntry(TraceEntry newEntry)
    {
        _logEntries.Add(newEntry);
    }

    public ObservableCollection<TraceEntry> GetLogEntries()
    {
        return _logEntries;
    }
}
于 2013-01-25T12:20:11.240 回答
0

正如@RobJohnson 提到的,您可以使用 ObservableCollection。但是,如果您需要创建自己的集合类,则应实现INotifyCollectionChanged接口:

public class TraceEntryQueue : INotifyCollectionChanged
{
    private readonly Queue<TraceEntry> _logEntries;

    public TraceEntryQueue()
    {
        _logEntries = new Queue<TraceEntry>();
    }

    public void AddEntry(TraceEntry newEntry)
    {
        _logEntries.Enqueue(newEntry);
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newEntry));
    }

    public List<TraceEntry> GetAsList()
    {
        return _logEntries.ToList();
    }

    public event NotifyCollectionChangedEventHandler CollectionChanged;

    protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        if (CollectionChanged != null)
        {
            CollectionChanged(this, e);
        }
    }
}
于 2013-01-25T12:12:30.940 回答
0
public class TraceEntryQueue
{
    private readonly Queue<TraceEntry> _logEntries; 
    private readonly ICollectionView _logEntriesView; 

    public TraceEntryQueue()
    {
        _logEntries = new Queue<TraceEntry>();
        _logEntriesView = CollectionViewSource.GetDefaultView(logEntries.ToList());
    }

    //....

    public void AddEntry(TraceEntry newEntry)
    {
        _logEntries.Enqueue(newEntry);
        _logEntriesView.SourceCollection = logEntries.ToList();
        _logEntriesView.Refresh();
    }

    public ICollectionView GetAsView()
    {
        return _logEntriesView;
    }
}

使用GetAsView并直接绑定到您的数据网格/列表框/列表视图。

于 2013-01-25T12:02:29.273 回答