I have a WPF application with two DataGrids that share the same ItemsSource. When I set one of the DataGrid\'s IsReadOnly property to true, I lose the ability to add recor
I think what's going on is that the IsReadOnly
property is making the DataGrid
readonly through the DefaultView for persons
, and since this DefaultView will be the same for both of your DataGrid's
, both looses the ability to add new rows.
Both doesn't become readonly however (as you said in your question) so I'm not sure if this is a bug or a desired behavior.
I'm also not sure what's going on behind the scenes here that causes this behavior but you can verify that the CollectionView's are the same through the debugger (since the CollectionView property is private). The following three statements come out as true
dgA.Items.CollectionView == CollectionViewSource.GetDefaultView(persons) // true
dgB.Items.CollectionView == CollectionViewSource.GetDefaultView(persons) // true
dgA.Items.CollectionView == dgB.Items.CollectionView // true
You can get it to work the way you like by changing the List
to an ObservableCollection
and use separate ListViewCollection's
for your DataGrid's
public MainWindow()
{
InitializeComponent();
ObservableCollection<Person> persons = new ObservableCollection<Person>();
persons.Add(new Person() { FirstName = "Bob", LastName = "Johnson" });
persons.Add(new Person() { FirstName = "John", LastName = "Smith" });
dgA.ItemsSource = new ListCollectionView(persons);
dgB.ItemsSource = new ListCollectionView(persons);
}