繁体   English   中英

Tabitem模板获取displayindex吗?

[英]Tabitem template get displayindex?

我想为我的Tabitem创建一个具有以下属性的样式:-第一个Tabitem将在左上角设置拐角半径-最后一个Tabitem将在右上角设置拐角半径

预期结果: 在此处输入图片说明

问题1:因此,我需要能够获取模板中当前tabitem的索引(以及tabcontrol中tabitem的数量)。

我希望能够以一种样式进行操作。 我目前正在使用3种样式(一种用于第一种,一种在最后一种,另一种在其他样式)来实现,但是在我的应用程序中,我经常不得不隐藏一个或两个表格,因此我需要检查是否必须设置一个新样式代码中的样式不是很有用。

问题2:我想在当前选定的Tabitem之前更改所有Tabitem的样式。

是否可以仅使用一种样式?

谢谢

3种样式部分没问题,您缺少的是一个StyleSelector ,它将根据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中将其用作:

 <TabControl ItemsSource="{Binding Items}" ItemContainerStyleSelector="{StaticResource TabItemStyleSelector}">
</TabControl>

请注意,上面的选择器适用于任何ItemsControl而不仅仅是TabControl

暂无
暂无

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

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