首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >INotifyCollectionChanged未更新UI

INotifyCollectionChanged未更新UI
EN

Stack Overflow用户
提问于 2013-04-30 18:45:19
回答 2查看 14.2K关注 0票数 3

我有一个如下所示的类。为简洁起见,我删除了所有函数

代码语言:javascript
复制
public class PersonCollection:IList<Person>
{}

现在我又多了一个Model类,如下所示。AddValueCommand是从ICommand派生的类,我再一次省略它。

代码语言:javascript
复制
public class DataContextClass:INotifyCollectionChanged
{
    private PersonCollection personCollection = PersonCollection.GetInstance();

    public IList<Person> ListOfPerson
    {
        get 
        {
            return personCollection;
        }            
    }

    public void AddPerson(Person person)
    {
        personCollection.Add(person);
        OnCollectionChanged(NotifyCollectionChangedAction.Reset);
    }


    public event NotifyCollectionChangedEventHandler CollectionChanged = delegate { };
    public void OnCollectionChanged(NotifyCollectionChangedAction action)
    {
        if (CollectionChanged != null)
        {
            CollectionChanged(this, new NotifyCollectionChangedEventArgs(action));
        }
    }       

    ICommand addValueCommand;

    public ICommand AddValueCommand
    {
        get
        {
            if (addValueCommand == null)
            {
                addValueCommand = new AddValueCommand(p => this.AddPerson(new Person { Name = "Ashish"}));
            }
            return addValueCommand;               
        }
    }
}

在主窗口中,我将UI绑定到Model,如下所示

代码语言:javascript
复制
 DataContextClass contextclass = new DataContextClass();           
 this.DataContext = new DataContextClass();

我的UI如下所示

代码语言:javascript
复制
<ListBox Margin="5,39,308,113" ItemsSource="{Binding Path=ListOfPerson}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Height="20" Text="{Binding Path=Name}"></TextBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <Button Content="Button"  HorizontalAlignment="Left" Command="{Binding Path=AddValueCommand}" Margin="233,39,0,73" />

单击按钮时,我的列表框不会更新为新值。我在这里错过了什么。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-30 18:47:02

INotifyCollectionChanged必须由集合类实现。而不是通过包含该集合的类。

您需要从DataContextClass中删除INotifyPropertyChanged并将其添加到PersonCollection中。

票数 11
EN

Stack Overflow用户

发布于 2013-04-30 18:50:08

不使用IList,而是使用ObservableCollection<T>并定义PersonCollection类,如下所示:

代码语言:javascript
复制
public class PersonCollection : ObservableCollection<Person>
{}

您可以阅读有关ObservableCollection<T>here的更多信息,该类是专门为WPF DataBinding场景中的集合更改通知而设计的。

从下面的MSDN中的定义可以看出,它已经实现了INotifyCollectionChanged

代码语言:javascript
复制
public class ObservableCollection<T> : Collection<T>, 
    INotifyCollectionChanged, INotifyPropertyChanged

下面是更多帮助你在WPF中使用ObservableCollection类的文章:

Create and Bind to an ObservableCollection

An introduction to ObservableCollection in Wpf

Databinding a ObservableCollection in MVVM

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

https://stackoverflow.com/questions/16297983

复制
相关文章

相似问题

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