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;
}
}
}