DataBound tabs: Selected index -1 after datacontext changes

6 posts, 1 answers
  1. Karl B
    Karl B avatar
    60 posts
    Member since:
    Dec 2013

    Posted 21 Mar Link to this post

    I am dynamically generating tabs based on databinding.  After I change the datacontext I get the tabs I expect but none of the tabs are selected. I am expecting a tab (likely the first one) to be selected when this happens.   I should mention I am using an older version R2 2017

     

    <CheckBox x:Name="cbox" Click="CheckBox_Checked">Click Me</CheckBox>
    <telerik:RadTabControl x:Name="tabCtrl" DisplayMemberPath="Name" ItemsSource="{Binding TabItems}" Height="250"/>
    private void TabCtrl_DataContextChanged (object sender, DependencyPropertyChangedEventArgs e)
        {
        // Attempting to fixt the problem here. Tabs are still unselected when changing data context
        this.tabCtrl.SelectedIndex = 0;
        }
     
    /// <summary>
    /// Simulating a data context change in a complicated MVVM scenario here
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void CheckBox_Checked (object sender, RoutedEventArgs e)
        {
     
        if ( cbox.IsChecked == true )
            {
            this.tabCtrl.DataContext = m_mainVm.TabViewModelA;
            }
        else
            {
            this.tabCtrl.DataContext = m_mainVm.TabViewModelB;
            }           
        }  

     

  2. Karl B
    Karl B avatar
    60 posts
    Member since:
    Dec 2013

    Posted 21 Mar Link to this post

    Here are my data classes I used-

    public class MyData
        {
        public String Name { get; set; }
        }
     
    public class TabControlViewModel
        {
        public List<MyData> TabItems { get; set; } = new List<MyData>();
        }
     
    public class MainViewModel
        {
        public TabControlViewModel TabViewModelA { get; set; } = new TabControlViewModel();
        public TabControlViewModel TabViewModelB { get; set; } = new TabControlViewModel();
     
        public MainViewModel ()
            {
     
            for ( int i = 0; i < 2; i++ )
                {
                TabViewModelA.TabItems.Add(new MyData { Name = "First" + i });               
                }
     
            for ( int i = 0; i < 3; i++ )
                {
                TabViewModelB.TabItems.Add(new MyData { Name = "Other" + i });
                }
            }
        }
  3. Martin Ivanov
    Admin
    Martin Ivanov avatar
    2090 posts

    Posted 26 Mar Link to this post

    Hello Karl,

    Thank you for the code. To achieve your requirement you can set the SelectedIndex to 0 in the Checked/Unchecked event handler. I've prepared a small example showing this. Can you please try it and let me know how it goes?

    Regards,
    Martin Ivanov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  4. Karl B
    Karl B avatar
    60 posts
    Member since:
    Dec 2013

    Posted 26 Mar in reply to Martin Ivanov Link to this post

    Martin Ivanov said:Hello Karl,

    Thank you for the code. To achieve your requirement you can set the SelectedIndex to 0 in the Checked/Unchecked event handler. I've prepared a small example showing this. Can you please try it and let me know how it goes?

    Regards,
    Martin Ivanov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.

     

    Going off of the checkbox handler will not work for me. As mentioned in the comments this is just simulating a complicated MVVM scenario. In my real scenario the data context of my view is changing based on treeview selections made in a separate view.

     

  5. Karl B
    Karl B avatar
    60 posts
    Member since:
    Dec 2013

    Posted 27 Mar in reply to Karl B Link to this post

    Do I need to create a sample project and submit a support ticket?
  6. Answer
    Martin Ivanov
    Admin
    Martin Ivanov avatar
    2090 posts

    Posted 29 Mar Link to this post

    Hello Karl,

    In this case you can use a dispatcher the delay the SelectedIndex setting in the DataContextChanged event handler. This will give the tabcontrol time to load its tabs before the selection appears. Here is an example in code:
    private void TabCtrl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        Dispatcher.BeginInvoke(new Action(() =>
        {
            this.tabCtrl.SelectedIndex = 0;
        }));
    }
    About the support ticket, if this doesn't help it would be very helpful to open a support ticket with a project. Also, the support ticket will guarantee you a reply within the 24h timeframe.

    Regards,
    Martin Ivanov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top