(MVVM) ListBox Binding

Posted 湖边的白杨树

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(MVVM) ListBox Binding相关的知识,希望对你有一定的参考价值。

当需要用Lisbbox 来log 一些记录的时候,ObservableCollection 并不可以是记录实时的反应在WPF 的UI上面。

这个时候就需要用一个异步collection 来完成。

    /// <summary>
    /// Represents the asynchronous observable collection.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class AsyncObservableCollection<T> : ObservableCollection<T>
    {
        /// <summary>
        /// The _synchronization context
        /// </summary>
        private readonly SynchronizationContext synchronizationContext = SynchronizationContext.Current;

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncObservableCollection{T}"/> class.
        /// </summary>
        public AsyncObservableCollection()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncObservableCollection{T}"/> class.
        /// </summary>
        /// <param name="list">The list.</param>
        public AsyncObservableCollection(IEnumerable<T> list)
            : base(list)
        {
        }

        /// <summary>
        /// Inserts the item.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="item">The item.</param>
        protected override void InsertItem(int index, T item)
        {
            this.ExecuteOnSyncContext(() => base.InsertItem(index, item));
        }

        /// <summary>
        /// Removes the item.
        /// </summary>
        /// <param name="index">The index.</param>
        protected override void RemoveItem(int index)
        {
            this.ExecuteOnSyncContext(() => base.RemoveItem(index));
        }

        /// <summary>
        /// Sets the item.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="item">The item.</param>
        protected override void SetItem(int index, T item)
        {
            this.ExecuteOnSyncContext(() => base.SetItem(index, item));
        }

        /// <summary>
        /// Moves the item.
        /// </summary>
        /// <param name="oldIndex">The old index.</param>
        /// <param name="newIndex">The new index.</param>
        protected override void MoveItem(int oldIndex, int newIndex)
        {
            this.ExecuteOnSyncContext(() => base.MoveItem(oldIndex, newIndex));
        }

        /// <summary>
        /// Clears the items.
        /// </summary>
        protected override void ClearItems()
        {
            this.ExecuteOnSyncContext(() => base.ClearItems());
        }

        /// <summary>
        /// Executes the on synchronize context.
        /// </summary>
        /// <param name="action">The action.</param>
        private void ExecuteOnSyncContext(Action action)
        {
            if (SynchronizationContext.Current == this.synchronizationContext)
            {
                action();
            }
            else
            {
                this.synchronizationContext.Send(_ => action(), null);
            }
        }
    } 

 另外还需要启用一个新的线程来更新collection

 

以上是关于(MVVM) ListBox Binding的主要内容,如果未能解决你的问题,请参考以下文章

Data Binding MVVM 数据绑定 总结

WPF MVVM模式,有两个ListBox和一个TxtBox,选任一个ListBox的Item ,就显示在TextBox上。请帮帮高手。。

MVVM:绑定到 ListBox.SelectedItem?

使用 MVVM 时,绑定不适用于 ListBox

将多选 ListBox 与 MVVM 同步

C# WPF MVVM Command Binding