This is a migrated thread and some comments may be shown as answers.

How many views are instantiated per TabControl?

2 Answers 37 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
Dr. Hartmut Kocher
Top achievements
Rank 1
Dr. Hartmut Kocher asked on 29 Mar 2010, 04:24 PM
Hi,
I have a TabControl where a list of my view model objects are bound.
The ContentTemplate uses a UserControl:

<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid Margin="-30,0,10,10">
<Views1:ControlUnitView Margin="10" />
                                    
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
I had expected that each tab item gets its own copy of the user control. This seems not to be the case. Instead only one user control is created but the data context is switched between each tab selection. Is this the expected behavior?

Within my user control, I have a grid. I want to copy the selected items from my view model to the SelectedItems property of the grid after the tab changed. I tried to use the datacontext-Changed event. However, when i set the SelectedItems list during this event, the items are not highlighted. But that's the only event I get within my user control. Is there a better way to accomplish this?
Thanks
Kind regards
Hartmut

2 Answers, 1 is accepted

Sort by
0
Accepted
Kiril Stanoev
Telerik team
answered on 31 Mar 2010, 09:47 AM
Hello Dr. Hartmut Kocher,

You are correct when you said that RadTabControl will create only one instance of you UserControl. This is by design.

The place where you add the items from your ViewModel to the SelectedItems of the GridView has to be called within a dispatcher.

private void GridView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    if (e.NewValue == null)
    {
        return;
    }
 
    Dispatcher.BeginInvoke(new Action(() =>
    {
        Department department = e.NewValue as Department;
        foreach (var item in department.Employees)
        {
            gridView1.SelectedItems.Add(item);
        }
    }), System.Windows.Threading.DispatcherPriority.ApplicationIdle);
}

I am attaching my sample project for further reference. Have a look at it and let me know if you find something unclear that needs explanation. I'd be glad to further assist you.

Regards,
Kiril Stanoev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Dr. Hartmut Kocher
Top achievements
Rank 1
answered on 31 Mar 2010, 01:10 PM
Thanks for the clarification.

I used a DispatcherTimer to overcome the problem but your solution with Dispatcher.BeginInvoke is even nicer!

Regards
Hartmut
Tags
TabControl
Asked by
Dr. Hartmut Kocher
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Dr. Hartmut Kocher
Top achievements
Rank 1
Share this question
or