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 records to the other DataGrid. I can still edit the contents of the second datagrid, but just cannot add records.
Is this intended? Is there way around this? I could use IsEnabled="False" for the DataGrid, but I then lose the ability to scroll in it.
Here is the setup:
XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<DataGrid Name="dgA" Grid.Row="0" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="FirstName" Binding="{Binding Path=FirstName}" />
<DataGridTextColumn Header="LastName" Binding="{Binding Path=LastName}" />
</DataGrid.Columns>
</DataGrid>
<DataGrid Name="dgB" Grid.Row="1" AutoGenerateColumns="False" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="FirstName" Binding="{Binding Path=FirstName}" />
<DataGridTextColumn Header="LastName" Binding="{Binding Path=LastName}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Person> persons = new List<Person>();
persons.Add(new Person() { FirstName = "Bob", LastName = "Johnson" });
persons.Add(new Person() { FirstName = "John", LastName = "Smith" });
dgA.ItemsSource = persons;
dgB.ItemsSource = persons;
}
class Person
{
public Person() { }
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
}
}
I think what's going on is that the
IsReadOnly
property is making theDataGrid
readonly through the DefaultView forpersons
, and since this DefaultView will be the same for both of yourDataGrid'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
You can get it to work the way you like by changing the
List
to anObservableCollection
and use separateListViewCollection's
for yourDataGrid's