0

I have created my own ExtendedTabControl to totally change it's apperance. It works ok. I have also created CloseableTabItem control. It works as well. However... so far I have manually put CloseableTabItem in my ExtendedTabControl this way:

<etc:ExtendedTabControl>            
    <etc:CloseableTabItem Header="First Item">
        <...some  content... />
    </etc:CloseableTabItem>            
</etc:ExtendedTabControl> 

Now I want to bind ObservableCollection to my ExtendedTabControl. Objects inside the Collection contains: header and contentText. I set ItemSource, ok, works. But how can I make the TabItems be my CloseableTabItems? What is more, how to bind my class objects to that CloseableTabItems? I Have noticed TabControl has a property called ItemTemplate, but setting it does not change it's apperance totally. There is still a background which I can't change. So an apperance works, but binding does not or the bindings work, but the apperance only partly. What can I do? Please help.

joni55
  • 3
  • 1

1 Answers1

0

In your ExtendedTabControl code you need to change the item container which is generated for items to your ClosableTabItem:

public class ExtendedTabControl : TabControl
{
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new CloseableTabItem();
    }
}

(There is a related method, IsItemItsOwnContainerOverride, which you might also want to override)

TabControls have two template you can set, the ContentTemplate which templates the item content area, and the ItemTemplate which should template the header.

To modify other properties on the generated tabs use the ItemContainerStyle, which now has a TargetType of ClosableTabItem.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Than you very much. Two more things... Now when user clicks on close button(on CloseableTabItem) I need to remove it's class object from ObservableCollection which is an ItemsSource. How can I get to it by an event raised in CloseableTabItem? And a similar problem. ExtendedTabControl has a button adding new Tab. I simply add new class object to the Collection but how to make it's CloseableTabItem be selected? – joni55 Jan 22 '12 at 13:55
  • @joni55: In the override event where you create the tab item you could subscribe to the close event already, and you have access to the itemssource as you are inside the ExtendedTabControl. To select the tab set the ExtendedTabControl.SelectedItem to the object you just added... – H.B. Jan 22 '12 at 14:28
  • One more time thank you. Autoselect done. The last thing which is about the first question: Setting ItemTemplate doesn't work. What I set as ItemTemplate is not shown in CloseableTabItem. In my logic, CloseableTabItem should contain sth like ContentPresenter(but not ContentPresenter, cuz it's about content), where the things I set in ItemTemplate are shown. I hope you know what I mean. What should I put in to that CloseableTabItem? – joni55 Jan 22 '12 at 14:48
  • @joni55: Check out the [default template](http://stackoverflow.com/questions/1559261/control-template-for-existing-controls-in-wpf) of `TabItems`, it should give you an idea how to bind things correctly. – H.B. Jan 22 '12 at 15:02