Using only one DataGrid is fine.
But in my sample code, I defined a checkbox (top left of window) to switch state of ReadOnly property of DataGrids that are in ListView items. ReadOnly state of ListView DataGrids are changing but there is side effects:
- The DataGrid that is defined before the ListBox is also changing its ReadOnly state.
- DataGrids that are defined in the ListBox are also following the state of ReadOnly property of the Datagrids defines in the ListView.
Updated after peoples told about duplicate: Just to let know peoples. The problem occurs because of usage of same ItemsSource. The same CollectionView will be used under the hood. Every functionalities related to CollectionView, for example the sort order or the IsReadOnly property will be affected on each Controls that has the same ItemsSource (Same CollectionView under the hood).
Why?
To reproduce the problem... Just do a normal WPF application and apply 2 modifications:
Change MainWindow xaml to:
<ScrollViewer> <Grid> <Grid.RowDefinitions> <RowDefinition Height ="Auto"></RowDefinition> <RowDefinition Height ="Auto"></RowDefinition> <RowDefinition Height ="Auto"></RowDefinition> <RowDefinition Height ="Auto"></RowDefinition> <RowDefinition Height ="Auto"></RowDefinition> <RowDefinition Height ="Auto"></RowDefinition> </Grid.RowDefinitions> <CheckBox Name="CheckBox"></CheckBox> <TextBlock Grid.Row="1" Text="{Binding Families.Count}"></TextBlock> <!--<Button Click="ButtonBase_OnClick"></Button>--> <DataGrid Grid.Row="2" ItemsSource="{Binding Familiy1.Persons}" IsReadOnly="True"> </DataGrid> <DataGrid Grid.Row="3" ItemsSource="{Binding Familiy1.Persons}" IsReadOnly="false"> </DataGrid> <ListBox Grid.Row="4" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding Families}" Background="Khaki"> <ListBox.ItemTemplate> <DataTemplate> <GroupBox Header="ListBox"> <DataGrid ItemsSource="{Binding Persons}"> </DataGrid> </GroupBox> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <ListView Grid.Row="5" ItemsSource="{Binding Families}" Margin="0,24,0,0" Background="Thistle" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <ListView.ItemTemplate> <DataTemplate> <GroupBox> <GroupBox.Header> <TextBlock Text="ListView"></TextBlock> </GroupBox.Header> <DataGrid ItemsSource="{Binding Persons}" Background="Red" AutoGenerateColumns="True" ColumnWidth="Auto" IsReadOnly="{Binding IsChecked , ElementName=CheckBox}"> </DataGrid> </GroupBox> </DataTemplate> </ListView.ItemTemplate> </ListView> </Grid> </ScrollViewer>
Also, add the following MainWindowModel.cs class:
using System.Collections.ObjectModel; using System.ComponentModel; namespace WpfDataGridIsReadOnlyBug { public class Person : INotifyPropertyChanged { private int _age; private string _name; public string Name { get { return _name; } set { _name = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name")); } } public int Age { get { return _age; } set { _age = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Age")); } } public Person() { } public Person(string name, int age) { Name = name; Age = age; } public event PropertyChangedEventHandler PropertyChanged; } public class Family : INotifyPropertyChanged { private string _name; public string Name { get { return _name; } set { _name = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name")); } } public Family() { } public Family(string name) { Name = name; } public ObservableCollection<Person> Persons { get; } = new ObservableCollection<Person>(); public event PropertyChangedEventHandler PropertyChanged; } public class MainWindowModel { public ObservableCollection<Family> Families { get; } = new ObservableCollection<Family>(); public Family Familiy1 { get { return Families[0]; } } public MainWindowModel() { Family family = new Family("Family 1"); family.Persons.Add(new Person("Eric", 50)); family.Persons.Add(new Person("Isabel Lucas", 35)); Families.Add(family); family = new Family("Family 2"); family.Persons.Add(new Person("Candice Swanopel", 25)); Families.Add(family); } } }