问题
for everyone, I found different problems with WPF, the TabControl and the DataGrid. Especially if the TabControl ItemsSource is bound.
Problems i found:
- Selection in DataGrid is not visible after switch Tabs back and forth
- DataGrid looses sorting on tab switch (SortDescriptions of CollectionView.GetDefaultCollection is cleared on unload)
- if a DataGrid cell has focus (is in edit mode) and you click on another tab, two things can happen: 1.) the bound object will not be updated; 2.) if the object is invalid you receive an error DeferRefresh not allowed during edit, or something like this
- DataGridComboBox and possibly other controls do clear their values if you switch to another tab if you are working with bound TabControls and DataTemplates. This clears any selection.
So now my question: Is there any ThirdParty controls which perform better in this scenarios?
You also can vote here http://connect.microsoft.com/VisualStudio/feedback/details/807849/databound-tabcontrol-bugs
I got answer from Microsoft it won't fix because not enough people have this problems. I know some fixes, but they are some really not clean (f.e. using reflection). Maybe you have some ideas?
回答1:
Hmmm, interesting post though I bet there are all not bugs. I think Microsoft hasnt even taken a look at those things. They might never do. I would appreciate it much if you could post or upload code of all those issues you might be thinking they are all buggy bugs.
Btw, what do you mean with TabControl ItemsSource is bound?
Here is my feedback on this from the info you gave us in the question. 1) You select something, you click away anywhere, no matter if tabitem or another window, you will lose focus, the selection will turn inactive means to slightly grey color. 2) Unloading means removing a control from VisualTree and so CollectionView must be cleared to release memory. This is good so since you dont want memory leaks. 3) If the cell's edit template contains controls which shall update the binding's source on focus lost then for sure that will happen. If you happen to be using a template for TabItems then the template will be mostly reused (means with same instance) and so you might end up taking the seat away from DataGrid' ass which is futhermore not a bug but rather something you wouldnt like to happen to you either. Therefore the DataGrid might be yelling "yo, no fooling around while I am editing a cell". 4) Same as in #3 it depends what you doing and how you define the templates. Consided mostly if template is in resources with a key then the template will be reused.
Just post us code please and let us take a look. I bet you might be doing something very "wpf-unlikely". :)
If those things really happen to be "buggies" (others review the same behavior), I bet there are workarounds for them. :)
Personally I have a feeling that all those things happen because you are using data bound TabControl. Whatever that might mean. I am excited to see what are data bound TabControls and how are they bound? How do you define those templates.
回答2:
I have the same issue.
Fix to DataGridComboBox issue may be to specify the ItemsSource of the ComboBox as the DataContext property of the TabControl instead of the DataGrid as the DataGrid is removed from the visual tree when you select another tab:
<TabControl x:Name="tabControl" Behaviours:TabContent.IsCached="True">
<TabItem Header="Tab1" Content="{Binding}" ContentTemplate="{StaticResource Tab1}"/>
<TabItem Header="Tab2" Content="{Binding}" ContentTemplate="{StaticResource Tab2}"/>
</TabControl>
<DataTemplate x:Key="Tab1">
<DataGrid ItemsSource="{Binding Entities}" x:Name="dataGrid">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="100"/>
<DataGridTemplateColumn Header="Position" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Position}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding Position, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Path=DataContext.Positions, ElementName=tabControl}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
来源:https://stackoverflow.com/questions/19876890/wpf-tabcontrol-and-datagrid-bugs-bugs-and-bugs