我已通过 ViewModel 属性将选项卡控件绑定(bind)到集合。我希望选项卡标题反射(reflect)它显示的项目的索引,但这似乎非常困难。是否有捷径可寻?
<TabControl
Style ="{StaticResource GtlTabControl}"
ItemsSource="{Binding Images}"
SelectedIndex="{Binding CurrentImageIndex}">
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="Header" Value="???"/>
</Style>
</TabControl.ItemContainerStyle>
...
</TabControl>
最佳答案
您可以在 xaml 中使用 AlternationIndex。像这样的东西:
<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>
这里还发布了替代方案:Show SelectedIndex in WPF Tabcontrol header template
关于c# - 索引 WPF 选项卡控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26869076/