问题
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