Silverlight TabControl - search and select TabItem from this control in TabItem

I am creating a LOB application that has a main section and a TabControl with various TabItems. On impact, the idea is retained that any fields with an error are highlighted, and the first field with an error gets focus.

If the first and only field with an error is on the "Unselected" tab, the tab should be selected, and the field with the error should be highlighted and have focus. But I can't get this to work.

What happens is that the Unselected tab is not in the visual tree, so you cannot go back to your own TabItem and make it the selected TabItem in TabControl.

Has anyone got an idea of โ€‹โ€‹how this can be done / achieved?

+6
silverlight tabcontrol tabitem
source share
5 answers

How I decided it (by asking the lead architect) ...

Create an ITabActivator interface in one way. Activate.

Create a class derived from Grid and ITabActivator called TabPageActivator. The constructor of which accepts TabITem and TabControl.

Instead of adding a simple table to TabItem.Contents add a TabPageActivator.

Change the definition of the parent to use ...

DependencyObject parent = _Control.Parent;

... instead of using VisualTreeHelper.

So, when you go to the hierarchy test for ...

if (parent - TabActivator) (parent as ITabActivator) .Activate ()

... therefore, when Activate is called

m_TabControl.SelectedItem = m_TabItem; // From the constructor parameters.

... and donโ€™t forget that you may have sub tabs, so you need to keep going in the hierarchy.

+1
source share

To download TabItem:

tabControl.SelectedItem = tabItemOfInterest; tabControl.UpdateLayout(); 

This causes tabItemOfInterest to be loaded along with all the contained controls in TabItem.

Only one line does not load tabItemOfInterest:

 tabControl.SelectedItem = tabItemOfInterest; 

However, I would be very interested in how David took the approach to mistaken control.

+4
source share

I use TabControls to navigate one of my sites ( YinYangMoney ) and have built several extension methods that help me select tabs using tag names. Here are the snippets that should work for you.

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<TabItem>() .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 will look something like this:

 <Controls:TabControl x:Name="x_TabControl"> <Controls:TabItem Header="Welcome" Tag="/Home/Welcome" x:Name="x_WelcomeTab" /> <Controls:TabItem Header="FAQ" Tag="/Home/FAQ" /> <Controls:TabItem Header="Contact Us" Tag="/Home/Contact_Us" /> <Controls:TabItem Header="Privacy Policy" Tag="/Home/Privacy_Policy" /> <Controls:TabItem Header="My Account" Tag="/Home/My_Account" /> </Controls:TabControl> 

And you can select Welcome TabItem as follows:

 x_TabControl.SelectTab("/Home/Welcome"); 

or

 x_TabControl.SelectTab(x_WelcomeTab); 
+1
source share

My solution using the attached TabItem property. Create the TabItemExtender class:

 /// <summary> /// TabItem Extender class with TabItem property /// </summary> public class TabItemExtender { #region property getters/setters /// <summary> /// TabItem attached dependency property /// </summary> public static readonly DependencyProperty TabItemProperty = DependencyProperty.RegisterAttached("TabItem", typeof(TabItem), typeof(TabItemExtender), null); /// <summary> /// TabItem Property getter /// </summary> public static TabItem GetNavigateUri(DependencyObject source) { return (TabItem)source.GetValue(TabItemExtender.TabItemProperty); } /// <summary> /// TabItem Property setter /// </summary> public static void SetNavigateUri(DependencyObject target, TabItem value) { target.SetValue(TabItemExtender.TabItemProperty, value); } #endregion } 

Next, do it in TabControl :

 private void ExtendedTabControl_Loaded(object sender, System.Windows.RoutedEventArgs e) { foreach (object item in this.Items) { var tabItem = item as TabItem; if (tabItem != null && tabItem.Content != null) { var element = (FrameworkElement)tabItem.Content; element.SetValue(TabItemExtender.TabItemProperty, tabItem); } } } 

and before adjusting the focus:

 var element = (UIElement)control; while (element != null) { //Get TabItem var tabItem = (TabItem)element.GetValue(TabItemExtender.TabItemProperty); if (tabItem != null) { if (!tabItem.IsSelected && tabItem.IsEnabled) { tabItem.IsSelected = true; ((TabControl)tabItem.Parent).UpdateLayout(); } break; } element = (UIElement)VisualTreeHelper.GetParent(element); } control.Focus(); 
+1
source share

I know the way, but it's ugly. It includes the use of DispatcherTimer at intervals of several milliseconds. In Page_Loaded you have to start a timer. Then, at each tick, it sets IsSelected = true for one of the tabs. At the next tick, he selects the next tab element, etc., until all the tabs are selected. Then you will need to select the first element again and kill the timer. This will make tabbed images load.

You will also have to cover the TabControl with a frame or something during this operation. Otherwise, the user will see how all tabs scroll quickly.

0
source share

All Articles