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

Load Tab Item Content on Tab Select Change

2 Answers 902 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
gerardo
Top achievements
Rank 1
Veteran
gerardo asked on 24 Feb 2021, 01:42 PM

Good day.

ControlTemplate of the RadTabControl defines a single ContentPresenter that holds only the currently selected RadTabItem's Content whice means Tab Item content will only be loaded when the Tab is selected.

I have this scenario where i have multiple Tab in a page. Tab 1 is the first tab opened when the page loaded. in my Tab 2, I have a UserControl with a GridView. I have a command Bound in the Loaded event of that UserControl. Now when the Page is loaded and shows the Tab 1, the Loaded command bound in the UserControl in Tab 2 is firing which I think the content of Tab 2 is also loaded?

Can someone explain me what is happening here?

Thank you very much.

2 Answers, 1 is accepted

Sort by
0
Accepted
Martin Ivanov
Telerik team
answered on 01 Mar 2021, 12:44 PM

Hello Gerardo,

The described behavior appears because the content of the second tab is included in the logical tree (the xaml) of the application. When the XAML parser goes through the elements in the logical tree it loads them which means that they are prepared for rendering. However, because second tab's content is not rendered because it is not included in the actual visual tree of the application. When this happens the Loaded event fires again.

To achieve your requirement, you can check if the element in the Content of the second tab has a visual parent. For example:

private void TextBlock_Loaded(object sender, RoutedEventArgs e)
{
	var textBlock = (TextBlock)sender;
	if (VisualTreeHelper.GetParent(textBlock) != null)
	{
		// do something
	}
}

Note that the previous example uses TextBlock, but you can use it whatever control is placed in the Content of the tab. Also, the example uses an event handler instead of a command as you described, but if you use the EventToCommandBehavior, you can pass the event arguments to the command execute handler. 

I hope this information helps. 

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
gerardo
Top achievements
Rank 1
Veteran
answered on 02 Mar 2021, 10:53 AM

Thank you for your reply Sir.

I used EventToCommandBehavior and the approach worked as what I needed.

Thank you.

Tags
TabControl
Asked by
gerardo
Top achievements
Rank 1
Veteran
Answers by
Martin Ivanov
Telerik team
gerardo
Top achievements
Rank 1
Veteran
Share this question
or