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

DataBound tabs: Selected index -1 after datacontext changes

5 Answers 42 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
Karl B
Top achievements
Rank 1
Karl B asked on 21 Mar 2019, 06:56 PM

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;
        }           
    }  

 

5 Answers, 1 is accepted

Sort by
0
Karl B
Top achievements
Rank 1
answered on 21 Mar 2019, 07:22 PM

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 });
            }
        }
    }
0
Martin Ivanov
Telerik team
answered on 26 Mar 2019, 09:43 AM
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.
0
Karl B
Top achievements
Rank 1
answered on 26 Mar 2019, 01:12 PM
[quote]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.

[/quote]

 

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.

 

0
Karl B
Top achievements
Rank 1
answered on 27 Mar 2019, 02:57 PM
Do I need to create a sample project and submit a support ticket?
0
Accepted
Martin Ivanov
Telerik team
answered on 29 Mar 2019, 12:45 PM
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.
Tags
TabControl
Asked by
Karl B
Top achievements
Rank 1
Answers by
Karl B
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or