3

我有一个列表框:

<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 时引发标志,而不是在其内容更改时。

4

3 回答 3

12

你的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));
    }


}
于 2013-09-11T02:30:35.273 回答
5

这就是ObservableCollection工作原理——它只监控项目的插入/删除/移动。

FileItem要在每个项目(或在您的情况下)更改时更新视图,必须在您设置要观察的每个属性时FileItem实现并触发适当的事件。INotifyPropertyChanged

这是如何执行此操作的示例:http: //msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

于 2013-09-11T02:25:43.067 回答
1

试试这个简单的:

public class NotifyObservableCollection<TItem> : ObservableCollection<TItem>
    where TItem : class , INotifyPropertyChanged, new()
{
    #region Fields

    private Action _itemPropertyChanged;

    #endregion

    #region Constructor

    public NotifyObservableCollection(Action itemPropertyChanged)
    {
        _itemPropertyChanged = itemPropertyChanged;
    }

    #endregion

    #region Methods

    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (var item in e.NewItems)
            {
                var notifyItem = item as INotifyPropertyChanged;
                if (notifyItem != null)
                {
                    notifyItem.PropertyChanged += ItemPropertyChanged;
                }
            }
        }
        else if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            foreach (var item in e.OldItems)
            {
                var notifyItem = item as INotifyPropertyChanged;
                if (notifyItem != null)
                {
                    notifyItem.PropertyChanged -= ItemPropertyChanged;
                }
            }
        }
        base.OnCollectionChanged(e);
    }

    #endregion

    #region Private Methods

    private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if(_itemPropertyChanged!=null)
        {
            _itemPropertyChanged();
        }
    }

    #endregion
}
于 2013-11-05T13:51:18.323 回答