-->

WPF: Two DataGrids, same ItemsSource, One IsReadOn

2020-02-29 12:05发布

问题:

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

回答1:

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