简体   繁体   English

索引WPF选项卡控件

[英]Indexing WPF tab controls

I have bound a tab control to a collection via a ViewModel property. 我已经通过ViewModel属性将选项卡控件绑定到集合。 I want the tab headers to reflect the index of the item it is showing, but this seems to be inordinately difficult. 我希望标签页的标题能够反映它显示的项目的索引,但这似乎非常困难。 Is there an easy way to do this? 是否有捷径可寻?

   <TabControl
            Style ="{StaticResource GtlTabControl}"
            ItemsSource="{Binding Images}"
            SelectedIndex="{Binding CurrentImageIndex}">
        <TabControl.ItemContainerStyle>
            <Style TargetType="TabItem">
                <Setter Property="Header" Value="???"/>
            </Style>
        </TabControl.ItemContainerStyle>
       ...
   </TabControl>

You can use an AlternationIndex inside the xaml. 您可以在xaml中使用AlternateIndex。 Something like this: 像这样:

<TabControl Style ="{StaticResource GtlTabControl}"
    ItemsSource="{Binding Images}"
    SelectedIndex="{Binding CurrentImageIndex}"
    AlternationCount="{Binding Path=Items.Count, RelativeSource={RelativeSource Self}}">
    <TabControl.ItemContainerStyle>
        <Style TargetType="TabItem">
            <Setter Property="Header" Value="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource  FindAncestor, AncestorType=TabItem}}"/>
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

There are also alternatives posted here: Show SelectedIndex in WPF Tabcontrol header template 这里还有其他替代方法: 在WPF Tabcontrol标头模板中显示SelectedIndex

If " Images " is a property of type List<MyImage> , then you can have an "Id" property in class "MyImage" and use it to bind to "Header" 如果“ Images ”是List<MyImage>类型的属性,则您可以在类“ MyImage”中具有“ Id”属性,并使用它绑定到“ Header”

public class MyImage{
  public int Id{get;set;}
}

....<TabControl.ItemContainerStyle>
            <Style TargetType="TabItem">
                <Setter Property="Header" Value="{Binding Path=Id}"/>
            </Style>
        </TabControl.ItemContainerStyle>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM