Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Even if you had access to the just the <code>TabItem</code> properties in the code-behind, it wouldn't help as the tab doesn't know it's own index in the <code>TabControl</code> collection. This is true of all <code>ItemsControls</code> and seems annoying, but it makes sense because when can any object ever tell you what its own position is within a collection?</p> <p>It can be done with IndexOf, however, as long as you have access to both the control's <code>ItemsCollection</code> and the content of the tab. We can do this in a <code>MultiValueConverter</code>, so that it can be done within the DataTemplate.</p> <p><strong>Converter code:</strong> </p> <pre><code> public class ItemsControlIndexConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { ItemCollection itemCollection = (ItemCollection)values[0]; return (itemCollection.IndexOf(values[1]) + 1).ToString(); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } </code></pre> <p><strong>TabControl XAML:</strong></p> <pre><code>&lt;TabControl ItemsSource="{Binding CoilItems}"&gt; &lt;TabControl.Resources&gt; &lt;local:ItemsControlIndexConverter x:Key="IndexConverter"/&gt; &lt;/TabControl.Resources&gt; &lt;TabControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding Converter="{StaticResource IndexConverter}" StringFormat="Order {0}" Mode="OneWay"&gt; &lt;Binding RelativeSource="{RelativeSource AncestorType=TabControl}" Path="Items"/&gt; &lt;!-- First converter index is the ItemsCollection --&gt; &lt;Binding /&gt; &lt;!-- Second index is the content of this tab --&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; &lt;!-- Fill in the rest of the header template --&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/TabControl.ItemTemplate&gt; &lt;/TabControl&gt; </code></pre>
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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