У меня есть класс, как показано ниже. Все функции я убрал для краткости
public class PersonCollection:IList<Person>
{}
Теперь у меня есть еще один класс Model, как показано ниже. AddValueCommand — это класс, производный от ICommand, который я снова опускаю.
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;
}
}
}
В главном окне я привязываю свой пользовательский интерфейс к модели, как показано ниже.
DataContextClass contextclass = new DataContextClass();
this.DataContext = new DataContextClass();
И мой пользовательский интерфейс выглядит так, как показано ниже.
<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" />
Мой список не обновляется новым значением при нажатии кнопки. Чего мне здесь не хватает.