I'm looking to create a new control template for a WPF tabcontrol.
The situation I have is that there will be a number of tabs in the control and the user will be able to add and delete these tabs.
Ideally I would like to create something along the lines of the Firefox / IE tabs whereby you have a delete button inside the tab heading to delete the current tab.开发者_运维百科
Also, and slightly more complicated, I want to have a new tab button that is always on the right hand side of the last tab in the tabcontrol.
Could anyone point me in a direction to get started? I've seen examples of people using Blend for this to create the new template. So far I've not been using that but have just downloaded the trial...
Thanks.
In this article (referred to MVVM) the author make use of control templates to style the tabs, with close button et al.
Or you can go more basic with this sample
The "new tab" button is more complicated. The main issue is that you want it to show up at the correct place in the ItemsPresenter/TabPanel but it isn't part of the collection driving the ItemsPresenter because it isn't actually a TabItem. I've done this in the past by modifying the ControlTemplate of the TabControl itself to stick the button onto the right side of the ItemsPresenter but you can end up with a weird look in situations where the tabs bump into a second row since the button isn't part of the TabPanel's layout.
Try the following articles; they have both helped me to get started writing a custom tab control.
1. Code Project: An Extended Tab Control
2. WPF TabControl Series
The first link provides a full custom solution which is really good but I found it a bit daunting at first. The second link provides a 4 part article which takes you right through re-templating the tab control.
How about using a TemplateSelector ?
Create two different templates - one for your normal tab items and one for your "new tab button". Both are actually tab items but with different templates depending on the conditions in your TemplateSelector.
To achieve different behavior / identify your "new tab button" just check in your code which template is assigned.
Hope that helps a bit.
精彩评论