首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BindingList<T>不会在编辑时触发ListChanged

BindingList<T>不会在编辑时触发ListChanged
EN

Stack Overflow用户
提问于 2019-09-15 00:12:37
回答 1查看 75关注 0票数 0

我的目标是创建一个列表,当元素中的元素发生变化时,该列表将触发事件。我的想法是创建一个实现INotifyChanged的实体的BindingList,以将该事件转发到ViewModel。

我目前所拥有的:

代码语言:javascript
复制
public class ViewModel
{
    public TagPresenter Tags {get;}
    public ViewModel()
    {
        Tags = new TagPresenter();

        Tags.TagCollection.ListChanged += (object o, ListChangedEventargs e) => { DataAccessor.UpdateTag(o[e.NewIndex]); };

        foreach(var tag in DataAccessor.GetTags())
            Tags.TagCollection.Add(new TagEntity(tag, Tags.TagCollection));
    }
}

public class TagPresenter
{
    public BindingList<object> TagCollection {get;}

    public TagPresenter()
    {
        TagCollection = new BindingList<object>();
    }
}

public class TagEntity : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged {get;}
    public Command ChangeState {get;}

    public TagEntity(string tag, BindingList<object> parent)
    {
        ChangeState = new Command(new Action(() => {
            NotifyPropertyChanged("Property");
        }));
    }

    public void NotifyPropertyChanged(string _property)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(_property));
    }
}

在这段代码中,ListChanged事件在foreach循环中的列表中添加新实体时触发,而不是在BindingList中触发实体的PropertyChanged时触发( NotifyPropertyChanged方法中的断点停止,但ListChanged事件不会触发)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-15 04:42:51

好了,弄清楚了,问题是由于在BindingList中对TagEntity对象进行装箱\取消装箱。一旦我添加了实现INotifyChanged的抽象类TagBase,并将集合切换到BindingList,它就可以按预期工作了:

代码语言:javascript
复制
public class ViewModel
{
    public TagPresenter Tags {get;}
    public ViewModel()
    {
        Tags = new TagPresenter();

        Tags.TagCollection.ListChanged += (object o, ListChangedEventargs e) => { DataAccessor.UpdateTag(o[e.NewIndex]); };

        foreach(var tag in DataAccessor.GetTags())
            Tags.TagCollection.Add(new TagEntity(tag, Tags.TagCollection));
    }
}

public class TagPresenter
{
    public BindingList<TagBase> TagCollection {get;}

    public TagPresenter()
    {
        TagCollection = new BindingList<TagBase>();
    }
}

public abstract class TagBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged {get;}

    public void NotifyPropertyChanged(string _property)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(_property));
    }
}

public class TagEntity : TagBase
{
    public Command ChangeState {get;}

    public TagEntity(string tag, BindingList<TagBase> parent)
    {
        ChangeState = new Command(new Action(() => {
            NotifyPropertyChanged("Property");
        }));
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57937226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档