코드를 통해 DataGrid와 상호 작용할 필요는 없습니다. 대신 DataGrid의 ItemsSource를 ObservableCollection에 바인딩하고 컬렉션을 조작하십시오. DataGrid가 자동으로 업데이트됩니다.
은 여기를보세요 :
Bind ObservableCollection to a DataGrid after assigning new value
사실, 당신은 (내 경우는 시간의 95 %이다), 당신이 당신의 WPF에 어떤 이름을 부여하지 않으려는 많은 경우에 그 실현합니다 제어. 코드가 UI에 대해 알지 못한다면 장기적으로 유지하는 것이 더 쉽습니다. 데이터 바인딩을 통해 정보를 가져올 위치를 UI에 알려주십시오. INotifyPropertyChanged 또는 Dependency Properties 및 DataContext를 살펴보십시오.
<DataGrid ItemsSource="{Binding MyCollection}" />
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
ObservableCollection<object> myCollection = new ObservableCollection<object>();
public ObservableCollection<object> MyCollection
{
get
{
return myCollection;
}
set
{
myCollection = value;
// Call the Notification here. Using linq and reflection you could get it to look like this:
// NotifyPropertyChanged(() => MyCollection);
}
}
myCollection 값을 실제로 변경하지 않으면 변경 알림을 제공 할 필요가 없습니다. ObservableCollection은 컬렉션을 수정할 때 실제로 자체적으로 변경 사항을 제공합니다.
출처
2012-11-10 14:13:26
Joe