TabControl 中的自定义 TabItem [英] Custom TabItem in TabControl

查看:28
本文介绍了TabControl 中的自定义 TabItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了继承自 TabItem 的 CustomTabItem,我想在 TabControl 中绑定 ObservableCollection 时使用它

I've created CustomTabItem which inherits from TabItem and i'd like to use it while binding ObservableCollection in TabControl

<TabControl ItemsSource="{Binding MyObservableCollection}"/>

在 XAML 中应该是这样,但我不知道如何在绑定时更改 TabControl 创建的输出项的默认类型.

It should like this in XAML, but i do not know how change default type of the output item created by TabControl while binding.

我尝试创建转换器,但它必须在 convertin 方法中执行以下操作:

I've tried to create converter, but it has to do something like this inside convertin method:

List<CustomTabItem> resultList = new List<CustomTabItem>();

并遍历我的输入 ObservableCollection,根据集合中的项目创建我的 CustomTab 并将其添加到 resultList...

And iterate through my input ObservableCollection, create my CustomTab based on item from collection and add it to resultList...

我想避免它,因为在创建 CustomTabItem 时我正在创建复杂的 View 并且需要一段时间,所以我不想在绑定集合中发生某些变化时总是创建它.

I'd like to avoid it, bacause while creating CustomTabItem i'm creating complex View and it takes a while, so i don't want to create it always when something change in binded collection.

我的类扩展了典型的 TabItem,我想在 TabControl 中使用这个类而不是 TabItem.

My class extends typical TabItem and i'd like to use this class in TabControl instead of TabItem.

        <TabControl.ItemContainerStyle>
            <Style TargetType="{x:Type local:CustomTabItem}">
                <Setter Property="MyProperty" Value="{Binding xxx}"/>
            </Style>
        </TabControl.ItemContainerStyle>

上面的代码生成错误,即样式无法应用于 TabItem.

Code above generates error that Style cannot be applied to TabItem.

我的主要目的是在 XAML 中使用我自己的 CustomTabItem 并绑定属性...就像上面一样...

My main purpose is to use in XAML my own CustomTabItem and bind properties... Just like above...

我也试过使用

<TabControl.ItemTemplate/>
<TabControl.ContentTemaplte/>

但它们只是 TabItem 的样式,所以我仍然会缺少我在自定义类中添加的属性.

But they are just styles for TabItem, so i'll still be missing my properties wchich i added in my custom class.

推荐答案

您将需要创建一个从 TabControl 派生的自定义类并覆盖 GetItemForContainerOverride 以返回您的自定义 TabItem:

You will need to create a custom class derived from TabControl and override GetItemForContainerOverride to return your custom TabItem:

protected override DependencyObject GetContainerForItemOverride()
{
  return new CustomTabItem();
}

这篇关于TabControl 中的自定义 TabItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆