0

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?

Daniel Bişar
  • 2,663
  • 7
  • 32
  • 54

2 Answers2

0

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.

dev hedgehog
  • 8,698
  • 3
  • 28
  • 55
  • I am going to post some code which will illustrate the behavior. But i will not be able to do this before this weekend. thank you for you post. P.S.: With DataBound TabControl i mean that the ItemSource is set to something like "{Binding Tabs}" and using a DataTemplate for each Tab. I will update my question above with details later. – Daniel Bişar Nov 13 '13 at 12:52
  • Hm it seems not so easy to reproduce as i thought. Maybe it has to do with some weird combinations in our production code... – Daniel Bişar Nov 21 '13 at 18:37
  • It's production code of the company i am working at. I am not allowed to upload that. I tried to reproduce this at home but until know have no luck. I'll let you know when i am able to reproduce the scenarios. Thanks for you offer! – Daniel Bişar Nov 21 '13 at 20:14
  • Then ask you company to pay me for showing up and solving all the issues. :) :) :) :) – dev hedgehog Nov 21 '13 at 20:17
0

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>
user1812267
  • 11
  • 1
  • 3