TabControl 中的自定义 TabItem

编程入门 行业动态 更新时间:2024-10-27 12:39:18
本文介绍了TabControl 中的自定义 TabItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我创建了继承自 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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 11:30:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408845.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   TabControl   TabItem

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!