Hi there!
I use XamGrid in my application and ran into the issue with cell editing when switching tabs in TabControl. Here is the markup on which the issue is reproduced:
<TabControl Grid.Row="0" Grid.Column="0"> <TabItem Header="First Tab"> <ig:XamGrid ItemsSource="{Binding Persons}"> <ig:XamGrid.EditingSettings> <ig:EditingSettings AllowEditing="Cell" IsMouseActionEditingEnabled="DoubleClick" IsF2EditingEnabled="True" /> </ig:XamGrid.EditingSettings> <ig:XamGrid.Columns> <ig:TemplateColumn Key="Name" HeaderText="Name" Width="200"> <ig:TemplateColumn.ItemTemplate> <DataTemplate DataType="{x:Type local:PersonViewModel}"> <TextBlock DataContext="{Binding}" Text="{Binding Name}" /> </DataTemplate> </ig:TemplateColumn.ItemTemplate> <ig:TemplateColumn.EditorTemplate> <DataTemplate DataType="{x:Type local:PersonViewModel}"> <TextBox DataContext="{Binding}" Text="{Binding Name}" /> </DataTemplate> </ig:TemplateColumn.EditorTemplate> </ig:TemplateColumn> </ig:XamGrid.Columns> </ig:XamGrid> </TabItem> <TabItem Header="Second Tab" /> </TabControl>
The XamGrid is in the TabItem and when I switch to another TabItem the content of the edited cell is cleared.
Steps to reproduce the issue:
Actual result: the edited cell is empty.
It seems that the cell DataContext binding is broken after exiting the edit mode and switching the tab.
Is there any workaround for this issue or maybe some XamGrid settings need to be added?
I have attached a test project on which the described issue is reproduced, please let me know if any additional information is required.
Thank you
Pavel
7026.TestXamGridApplication.zip
Hello Pavel,
I've determined the root cause is setting a binding on your TextBlock's DataContext. eg. DataContext="{Binding}"
Please remove this and the behavior will go away. I am not sure what the intention was, but setting DataContext is quite often unnecessary since it's already set up for you from the data template. The current binding context will automatically have its DataContext property set to the equivalent of {Binding}.
Let me know if you have any questions.
Michael,
Thank you for the answer, the proposed solution works in the example above!
In real application I use more complex controls in the Item and Editor templates and these controls require setting of DataContext property. So, the proposed solution does not work for me in real application.
Also, I noticed that the content of the empty cell starts to be displayed when I enter and exit the edit mode again.
Is there any other solution to the described issue without removing the DataContext setting?