2010-11-18 4 views
0

を変更した後、正常に描画されていません。ここに私のコードがあります:WPFデータグリッドを基になるコレクションに

<Grid> 
    <Button Content="Button" Click="button1_Click" /> 
    <DataGrid ItemsSource ="{Binding Lst}" /> 
</Grid> 

コードビハインド:

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     (this.DataContext as Some).remove(); 
    } 

データソースは次のとおりです。

public class Some : INotifyPropertyChanged 
{ 
    private List<Point> lst = new List<Point>(); 
    public List<Point> Lst 
    { 
     get 
     { 
      return lst; 
     } 
    } 

    public Some() 
    { 
     lst.Add(new Point(2.3, 5)); 
     lst.Add(new Point(267.3, 5)); 
     lst.Add(new Point(2.3, 65)); 
     lst.Add(new Point(2.63, 885)); 
     lst.Add(new Point(27.3, 65)); 

    } 
    public void remove() 
    { 
     lst.Remove(lst.Last()); 
     if (PropertyChanged != null) 
      PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Lst")); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

私はremove()メソッドを呼び出します、私はコレクションからアイテムを削除し、propertychangedを呼び出します。 UI反応:私は正しく削除されたPointに対応するデータグリッドのセルを選択できません。それらはではなく、が削除されています。これはUIバグのようですが、回避策はありますか?

申し訳ありませんが汚れています。ちょっとしたサンプルです。

おかげで、イリヤ

答えて

2

Lstのために使用ObservableCollection<>の代わりList<> - 追加するとき、コレクションの変更を通じてのObservableCollectionを自動的に通知し、削除またはクリアします。また、DependencyPropertyが必要です。http://forums.silverlight.net/forums/t/12664.aspx

+0

ありがとうございました!今私はObservableCollectionが使用されていることを知っています:) –

+0

依存関係のプロパティは必要ありません。 INotifyPropertyChangedインターフェイスとINotifyCollectionChangedインターフェイスを実装するだけで、ビューモデルを使用できます。 – Bulat

1

ビューモデルにItemSourceをバインドする場合、コレクションにINotifyCollectionChangedを実装する必要があります。 ObsevableCollectionは、実際にはINotifyCollectionChangedを実装するコレクションであり、WPFコントロールに追加および削除された要素を通知します。

関連する問題