C# Tabitem模板是否获取displayindex?
我想为我的tabitems创建具有以下属性的样式: -第一个选项卡项将在左角设置角半径 -最后一个选项卡项将在右拐角处设置拐角半径 预期结果: 问题1: 因此,我需要能够获得模板中当前tabitem的索引(以及tabcontrol中tabitem的数量) 我希望能够用一种样式来完成。我目前使用3种样式(一种用于第一种,一种用于最后一种,另一种用于其他),但在我的应用程序中,我经常需要隐藏一个或两个tabitems,因此我需要检查是否必须在代码中设置一种新的样式,这不是真正有用的 问题2:我想在当前选定的tabitem之前更改所有tabitems的样式 这是否可能只使用一种样式C# Tabitem模板是否获取displayindex?,c#,wpf,C#,Wpf,我想为我的tabitems创建具有以下属性的样式: -第一个选项卡项将在左角设置角半径 -最后一个选项卡项将在右拐角处设置拐角半径 预期结果: 问题1: 因此,我需要能够获得模板中当前tabitem的索引(以及tabcontrol中tabitem的数量) 我希望能够用一种样式来完成。我目前使用3种样式(一种用于第一种,一种用于最后一种,另一种用于其他),但在我的应用程序中,我经常需要隐藏一个或两个tabitems,因此我需要检查是否必须在代码中设置一种新的样式,这不是真正有用的 问题2:我想在当
谢谢3种样式部分没有问题,您缺少的是一个
样式选择器
,它将根据ItemIndex选择样式
public class TabItemStyleSelector : StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
var itemsControl = ItemsControl.ItemsControlFromItemContainer(container);
var itemIndex = itemsControl.ItemContainerGenerator.IndexFromContainer(container);
//first index
if(itemIndex == 0)
{
return (Style)itemsControl.FindResource("FirstTabItemItemStyle");
}
//last index
if (itemIndex == itemsControl.Items.Count - 1)
{
return (Style)itemsControl.FindResource("LastTabItemItemStyle");
}
//other indecies
return (Style)itemsControl.FindResource("OtherTabItemItemStyle");
//return base.SelectStyle(item, container); return this if OtherTabItemItemStyle does not exist
}
}
将其添加到您的资源中
<Window.Resources>
<local:TabItemStyleSelector x:Key="TabItemStyleSelector" />
</Window.Resources>
注意:上述选择器适用于任何项控件
而不仅仅是选项卡控件
<TabControl ItemsSource="{Binding Items}" ItemContainerStyleSelector="{StaticResource TabItemStyleSelector}">
</TabControl>