Custom TabItem in TabControl

笑着哭i 提交于 2020-01-17 01:09:10

问题


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

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

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

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

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

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

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.

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>

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

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

I've also tried to use

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

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


回答1:


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();
}


来源:https://stackoverflow.com/questions/2660211/custom-tabitem-in-tabcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!