INotifyCollectionChanged is not updating the UI

情到浓时终转凉″ 提交于 2020-01-05 21:31:22

问题


I have a class as shown below. All the functions I have removed for brevity

public class PersonCollection:IList<Person>
{}

Now I have one more Model class as shown below. AddValueCommand is class deriving from ICommand which again I am omiting.

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;               
        }
    }
}

In the main window I am binding my UI to Model as shown below

 DataContextClass contextclass = new DataContextClass();           
 this.DataContext = new DataContextClass();

And My UI looks like as shown below

<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" />

My List Box is not updating with the new value when button is clicked. What I am missing here.


回答1:


INotifyCollectionChanged has to be implemented by a collection class. Not by the class containing the collection.
You need to remove INotifyPropertyChanged from DataContextClass and add it to PersonCollection.




回答2:


Instead of using IList use ObservableCollection<T> and define your PersonCollection class as below:

public class PersonCollection : ObservableCollection<Person>
{}

You can read more about ObservableCollection<T> class here which is designed specifically for collection change notifications in WPF DataBinding scenarios.

As you can see from the definition in MSDN below, it already implements INotifyCollectionChanged

public class ObservableCollection<T> : Collection<T>, 
    INotifyCollectionChanged, INotifyPropertyChanged

More articles to help you with usage of ObservableCollection class in WPF are below:

Create and Bind to an ObservableCollection
An introduction to ObservableCollection in Wpf
Databinding a ObservableCollection in MVVM



来源:https://stackoverflow.com/questions/16297983/inotifycollectionchanged-is-not-updating-the-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!