ReadWrite & ReadOnly datagrid linked to a common source, resulting in ReadWrite not matching ReadWrite?

I found something strange: I have a form with two datagrids linked to the same collection. Depending on the order of the datagrid in Xaml, the behavior is different.

This works as expected (added extra line to add):

<DockPanel> <DockPanel DockPanel.Dock="Right"> <Label Content="ReadOnlyView" DockPanel.Dock="Top"/> <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="True" /> </DockPanel> <DockPanel> <Label Content="EditorView" DockPanel.Dock="Top" /> <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="False" CanUserAddRows="True" /> </DockPanel> </DockPanel> 

Arranging xaml this way is what confuses me (no extra line to add)

 <DockPanel> <DockPanel> <Label Content="EditorView" DockPanel.Dock="Top" /> <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="False" CanUserAddRows="True" /> </DockPanel> <DockPanel DockPanel.Dock="Right"> <Label Content="ReadOnlyView" DockPanel.Dock="Top"/> <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="True" /> </DockPanel> </DockPanel> 

Below is the dummy ViewModel that I used to do this:

 public class PersonsViewModel { public PersonsViewModel() { Persons = new ObservableCollection<Person> { new Person {Name = "Johan"}, new Person {Name = "Dave"}, }; } public ObservableCollection<Person> Persons { get; private set; } } public class Person { public string Name { get; set; } } 

The question is, what is the reason for this behavior?

+4
source share
1 answer

Great question Johan! I assume that since you are not explicitly providing it with a CollectionViewSource , the automatically generated cvs of the DataGrid are shared between them, as you are referring to the same source.

Therefore, the last installation wins when you perform two IsReadOnly and are a common source, both DataGrid show the same effect.

To confirm my assumption, I used this code, and DataGrids behaves as you would expect when you offer them an explicit CollectionViewSource to work with.

 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <CollectionViewSource Source="{Binding Persons}" x:Key="cvs1" /> <CollectionViewSource Source="{Binding Persons}" x:Key="cvs2" /> </Window.Resources> <DockPanel> <DataGrid ItemsSource="{Binding Source={StaticResource ResourceKey=cvs1}}" IsReadOnly="False" CanUserAddRows="True" /> <DataGrid ItemsSource="{Binding Source={StaticResource ResourceKey=cvs2}}" IsReadOnly="True" /> </DockPanel> </Window> 

EDIT: Further testing indicates that the behavior can simply be called odd! I can't explain why this produces three read-only DGs

 <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="True" /> <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="False" /> <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="True" /> 

but this creates an alternate readonly and editable DG:

 <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="True" /> <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="False" /> <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="True" /> <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="False" /> 

So, I think the aforementioned CVS is best described as a workaround for this odd behavior, so you can achieve what you really want.

EDIT 2: After even more combinations of true false, the only thing I noticed is that if the Last IsReadOnly in DataGrid parameter is set to True, all other DataGrids become read-only. But if the latter is set to false, then all other DataGrids behave in accordance with their IsReadOnly setting. This may be due to this MSDN bit.

 If a conflict exists between the settings at the DataGrid, column, or cell levels, a value of true takes precedence over a value of false. 
+2
source

All Articles