다음과 같은 클래스가 있습니다. 아래 그림과 같이 내가 INotifyCollectionChanged가 UI를 업데이트하지 않습니다.
public class PersonCollection:IList<Person>
{}
간결
제거 한 모든 기능은 지금은 또 하나 개의 모델 클래스가 있습니다. 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();
아래 그림 그리고 내 UI가 함께 업데이트되지
<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" />
내 목록 상자 아래와 같이처럼 보이는 모델 내 UI를 결합하고 버튼이 클릭되었을 때의 새로운 값 내가 여기서 누락 된 것.
Answers에 대해 고마워,하지만 여기에 내 PersonCollection을 변경하고 싶지 않다면 어떻게 될까? DataContextClass에서 변경 작업 만 수행하면 어떻게 할 수 있습니까? – Vikram
@Vikram : 할 수 없습니다. WPF 엔진은 'ItemsSource'에 바인딩 된 속성에서 해당 인터페이스를 찾습니다. 데이터 컨텍스트에서 해당 인터페이스를 찾지 않습니다. –