を変更した後、正常に描画されていません。ここに私のコードがあります: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バグのようですが、回避策はありますか?
申し訳ありませんが汚れています。ちょっとしたサンプルです。
おかげで、イリヤ
ありがとうございました!今私はObservableCollectionが使用されていることを知っています:) –
依存関係のプロパティは必要ありません。 INotifyPropertyChangedインターフェイスとINotifyCollectionChangedインターフェイスを実装するだけで、ビューモデルを使用できます。 – Bulat