加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

WPF / C#将ObservableCollection中项的属性更改更新为ListBox

发布时间:2020-12-16 00:09:11 所属栏目:百科 来源:网络整理
导读:我有一个列表框: ListBox x:Name="lbxAF" temsSource="{Binding}" 从这个修改过的Observable Collection中获取数据: public ObservableCollectionExFileItem folder = new ObservableCollectionExFileItem(); 这是在使用FileSystemWatcher监视特定文件夹以
我有一个列表框:

<ListBox x:Name="lbxAF" temsSource="{Binding}">

从这个修改过的Observable Collection中获取数据:

public ObservableCollectionEx<FileItem> folder = new ObservableCollectionEx<FileItem>();

这是在使用FileSystemWatcher监视特定文件夹以添加,删除和修改文件的类中创建的.

ObservableCollection被修改了(因此最后是Ex),所以我可以从外部线程修改它(代码不是我的,我实际上通过这个网站搜索并发现它,就像一个魅力):

// This is an ObservableCollection extension
    public class ObservableCollectionEx<T> : ObservableCollection<T>
    {
        // Override the vent so this class can access it
        public override event System.Collections.Specialized.NotifyCollectionChangedEventHandler CollectionChanged;

        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            using (BlockReentrancy())
            {
                System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHanlder = CollectionChanged;
                if (eventHanlder == null)
                    return;

                Delegate[] delegates = eventHanlder.GetInvocationList();

                // Go through the invocation list
                foreach (System.Collections.Specialized.NotifyCollectionChangedEventHandler handler in delegates)
                {
                    DispatcherObject dispatcherObject = handler.Target as DispatcherObject;

                    // If the subscriber is a DispatcherObject and different thread do this:
                    if (dispatcherObject != null && dispatcherObject.CheckAccess() == false)
                    {
                        // Invoke handler in the target dispatcher's thread
                        dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind,handler,this,e);
                    }
                    // Else,execute handler as is
                    else
                    {
                        handler(this,e);
                    }
                }
            }
        }
    }

该系列由以下组成:

public class FileItem
{
    public string Name { get; set; }
    public string Path { get; set; }
}

这允许我存储文件的名称和路径.

就删除和添加文件而言,一切都很有效,并且列表框可以完美地更新这两个…但是,如果我更改任何文件的名称,它不会更新列表框.

如何通知列表框中FileItem属性的更改?我假设ObservableCollection将处理它,但显然它只在添加或删除FileItem时引发标志,而不是在其内容被更改时引发.

解决方法

您的FileItem类应实现INotifyPropertyChanged.下面是一个简单的工作实现.

public class FileItem : INotifyPropertyChanged
{
    private string _Name;

    public string Name
    {
        get { return _Name; }
        set {
            if (_Name != value)
            {
                _Name = value;
                OnPropertyChanged("Name");
            }
        }
    }

    private string _Path;

    public string Path
    {
        get { return _Path; }
        set {
            if (_Path != value)
            {
                _Path = value;
                OnPropertyChanged("Path");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
    }


}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读