How many views are instantiated per TabControl?

3 posts, 1 answers
  1. Dr. Hartmut Kocher
    Dr. Hartmut Kocher avatar
    6 posts
    Member since:
    Dec 2009

    Posted 29 Mar 2010 Link to this post

    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. Answer
    Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 31 Mar 2010 Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Dr. Hartmut Kocher
    Dr. Hartmut Kocher avatar
    6 posts
    Member since:
    Dec 2009

    Posted 31 Mar 2010 Link to this post

    Thanks for the clarification.

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

    Regards
    Hartmut
Back to Top