Xavier Peña
Xavier Peña

Reputation: 7919

Trying to understand different behavior in binding for DataGrid between WindowsForms and WPF

In Windows Forms I used:

dataGridView.DataSource = new BindingList<MyItem>();

The equivalent in WPF seems to be:

dataGrid.ItemsSource = new BindingList<MyItem>();

What I don't understand (and perhaps I'm doing something wrong) is that in the WPF case, the binding doesn't seem to be bidirectional. That is to say: when I modify a MyItem, it is not automatically reflected in the view.

Upvotes: 0

Views: 34

Answers (1)

  1. Use ObservableCollection<MyItem>.

  2. MyItem must implement INotifyPropertyChanged and raise PropertyChanged when any of its property values changes.

That'll update the grid cells.

  1. If you plan to do anything much with WPF, learn MVVM and use Binding. Assigning a collection to a property doesn't bind it.

XAML:

<DataGrid
    x:Name="dataGrid"
    ItemsSource="{Binding MyItemCollection}"
    ... 
    />

You can also create a Binding programmatically, if you really want to make a lot of extra work for yourself.

Seems like ObservableCollection has some improvements over BindingList.

Upvotes: 1

Related Questions