Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to give you a solution using attached properties. Check the code:</p> <p><strong>Attached Properties</strong></p> <pre><code>public static class IndexAttachedProperty { #region TabItemIndex public static int GetTabItemIndex(DependencyObject obj) { return (int) obj.GetValue(TabItemIndexProperty); } public static void SetTabItemIndex(DependencyObject obj, int value) { obj.SetValue(TabItemIndexProperty, value); } // Using a DependencyProperty as the backing store for TabItemIndex. This enables animation, styling, binding, etc... public static readonly DependencyProperty TabItemIndexProperty = DependencyProperty.RegisterAttached("TabItemIndex", typeof (int), typeof (IndexAttachedProperty), new PropertyMetadata(-1)); #endregion #region TrackTabItemIndex public static bool GetTrackTabItemIndex(DependencyObject obj) { return (bool) obj.GetValue(TrackTabItemIndexProperty); } public static void SetTrackTabItemIndex(DependencyObject obj, bool value) { obj.SetValue(TrackTabItemIndexProperty, value); } // Using a DependencyProperty as the backing store for TrackTabItemIndex. This enables animation, styling, binding, etc... public static readonly DependencyProperty TrackTabItemIndexProperty = DependencyProperty.RegisterAttached("TrackTabItemIndex", typeof (bool), typeof (IndexAttachedProperty), new PropertyMetadata(false, TrackTabItemIndexOnPropertyChanged)); private static void TrackTabItemIndexOnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var tabControl = GetParent(d, p =&gt; p is TabControl) as TabControl; var tabItem = GetParent(d, p =&gt; p is TabItem) as TabItem; if (tabControl == null || tabItem == null) return; if (!(bool)e.NewValue) return; int index = tabControl.Items.IndexOf(tabItem.DataContext == null ? tabItem : tabItem.DataContext); SetTabItemIndex(d, index); } #endregion public static DependencyObject GetParent(DependencyObject item, Func&lt;DependencyObject, bool&gt; condition) { if (item == null) return null; return condition(item) ? item : GetParent(VisualTreeHelper.GetParent(item), condition); } } </code></pre> <p>This code define two attached properties, the first one is to set if an item tracks the the tab item index in wich it is contained. The second one is the index property.</p> <p><strong>XAML Sample code:</strong></p> <pre><code> &lt;TabControl.ItemTemplate&gt; &lt;DataTemplate DataType="{x:Type WpfApplication3:A}"&gt; &lt;StackPanel x:Name="tabItemRoot" WpfApplication3:IndexAttachedProperty.TrackTabItemIndex ="True"&gt; &lt;TextBlock Text="{Binding Text}"/&gt; &lt;TextBlock Text="{Binding Path=(WpfApplication3:IndexAttachedProperty.TabItemIndex), ElementName=tabItemRoot}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/TabControl.ItemTemplate&gt; </code></pre> <p>The above code is an example of using the attached property. You may adapt to your code easy.</p> <p><strong>Result:</strong></p> <p><img src="https://i.stack.imgur.com/ENgsI.png" alt="enter image description here"></p> <p>Hope this code works for you...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload