Silverlight TabControl - Finding and selecting a TabItem from a given Control in the TabItem?

I know a way, but it is ugly. It involves using a DispatcherTimer with an interval of a few milliseconds. In Page_Loaded you would start the timer.

Then on each tick it sets IsSelected = true for one of the tab items. On the next tick it selects the next tab item etc. Until all the tabs have been selected. Then you would have to select the first item again and kill the timer.

This will force the visuals in the tab items to load You would also have to cover the TabControl with a border or something during this operation. Otherwise the user will see all the tab items quickly flicking past.

I know a way, but it is ugly. It involves using a DispatcherTimer with an interval of a few milliseconds. In Page_Loaded you would start the timer.

Then on each tick it sets IsSelected = true for one of the tab items. On the next tick it selects the next tab item etc. Until all the tabs have been selected. Then you would have to select the first item again and kill the timer.

This will force the visuals in the tab items to load. You would also have to cover the TabControl with a border or something during this operation. Otherwise the user will see all the tab items quickly flicking past.

Yeah - not a pretty solution. – David Gray Wright May 4 '10 at 4:00 True. I use this solution myself, and it makes me cringe every time I look at the code.

:) If I were to do it again I would encapsulate it in a behavior to get it out of sight. Unfortunately I cannot think of another way to make the visuals render. – Henrik Söderlund May 4 '10 at 7:56.

How I solved it (by asking the Lead Architect)... Create an Interface ITabActivator with one method Activate. Create a class derived from Grid and ITabActivator called TabPageActivator. The constructor of which takes the TabITem and the TabControl.

Instead of adding a simple Grid to the TabItem. Contents add a TabPageActivator. Change the Parent detection to use... DependencyObject parent = _Control.

Parent; ...instead of using the VisualTreeHelper. So when you navigate the .. if ( parent is TabActivator ) (parent as ITabActivator). Activate( ) ... so when Activate is called m_TabControl.

SelectedItem = m_TabItem; // From Constructor Parameters. ...and don't forget you may have nested tabs so you need to keep going up the .

– Chris S Jul 1 '10 at 12:03 No it doesn't, it works fine. I was maybe a little too brief in my explanation of the solution. I can explain in more detail if you'd like?

– David Gray Wright Jul 2 '10 at 3:34 small nitpick: I found it a really bad idea to use non-default constructors on controls. It makes the control non-xaml friendly... – TDaver Aug 10 at 8:37.

I use TabControls for navigation on one of my sites (YinYangMoney) and built a few extension methods that help me to select tabs using Tag names. Here are snippets that should work for you. The Extensions class: using System; using System.

Linq; using System.Windows. Controls; namespace MyApplication { internal static class Extensions { // Extension for TabControl internal static void SelectTab(this TabControl tabControl, this TabItem tabItem) { if (tabControl == null || tabItem == null) return null; SelectTab(tabControl, tabItem. Tag); } // Extension for TabControl internal static void SelectTab(this TabControl tabControl, string tabTagName) { if (tabControl == null) return null; // Find the TabItem by its Tag name TabItem mainTabItem = tabControl.

FindByTag(tabTagName); if (mainTabItem == null) return; // Check to see if the tab needs to be selected if (tabControl. SelectedItem! = mainTabItem) tabControl.

SelectedItem = mainTabItem; } // Extension for TabControl internal static TabItem FindByTag(this TabControl tabControl, string tagFragment) { if (tabControl == null || tagFragment == null) return null; return tabControl. Items .OfType() . Where(item => item.

Tag! = null && item.Tag.ToString(). StartsWithIgnoreCase(tagFragment)) .FirstOrDefault(); } // Extension for string internal static bool StartsWithIgnoreCase(this string source, string target) { return source.

StartsWith(target, StringComparison. CurrentCultureIgnoreCase); } } } The XAML for your TabControl and TabItems would look something like this: And you can select the Welcome TabItem like so: x_TabControl. SelectTab("/Home/Welcome"); or x_TabControl.

SelectTab(x_WelcomeTab).

The problem is finding the TabItem that is to be selected based on a control that will be shown within it. – David Gray Wright May 4 '10 at 7:13.

To load the TabItem: tabControl. SelectedItem = tabItemOfInterest; tabControl.UpdateLayout(); This causes the tabItemOfInterest to load alongwith all the contained controls within the TabItem. The below line alone does not loads the tabItemOfInterest: tabControl.

SelectedItem = tabItemOfInterest; I would, however, be very interested in the approach David adopted to get to the erroneous control.

The constructor of which takes the TabITem and the TabControl. Instead of adding a simple Grid to the TabItem. Contents add a TabPageActivator.

DependencyObject parent = _Control. ...instead of using the VisualTreeHelper. (parent as ITabActivator).

SelectedItem = m_TabItem; // From Constructor Parameters. ...and don't forget you may have nested tabs so you need to keep going up the Hierarchy.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions