While I don't actually have the coded solution, I can give some insight on what is most likely the appropriate way to handle this in a WPF/MVVM pattern.
Firstly, if we break down the request it is as follows:
- You have a sequence of elements that you want to display.
- You want the user to be able to remove an individual element from the sequence.
- You want the user to be able to add a new element to the sequence.
Additionally, since you are attempting to use a TabControl
, you are also looking to get the behavior that a Selector
control provides (element selection), as well as an area to display the element (content) which is selected.
So, if we stick to these behaviors you'll be fine, since the user interface controls can be customized in terms of look and feel.
Of course, the best control for this is the TabControl
, which are you already trying to use. If we use this control, it satisfies the first item.
<TabControl ItemsSource="{Binding Path=Customers}" />
Afterwards, you can customize each element, in your case you want to add a Button
to each element which will execute a command to remove that element from the sequence. This will satisfy the second item.
<TabControl ...>
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=CustomerId}" />
<Button Command="{Binding Path=RemoveItemCommand, Mode=OneTime,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type TabControl}}"
CommandParameter="{Binding}" />
</StackPanel>
</DataTemplate>
<TabControl.ItemTemplate>
</TabControl>
The last part is a bit more difficult, and will require you to actually have to create a custom control that inherits from the TabControl
class, add an ICommand
DependencyProperty
, and customize the control template so that it not only displays the TabPanel
, but right next to it also displays a Button
which handles the DependencyProperty
you just created (the look and feel of the button will have to be customized as well). Doing all of this will allow you to display your own version of a TabControl
which has a faux TabItem
, which of course is your "Add" button. This is far far far easier said than done, and I wish you luck. Just remember that the TabPanel
wraps onto multiple rows and can go both horizontally or vertically. Basically, this last part is not easy at all.