I have TabControl
that has already define some TabItems
on XAML
. I need to create new TabItems
and add to it.
If I use ItemSource
I get an exception Items collection must be empty before using ItemsSource.
The solution I have found so far is to create those TabItems
I have already defined on XAML
but programmatically on the ViewModel, so I can created the others I really need, but doesn't seems to be a good solution.
Other solution would be to add the TabControl
as a property and use the Code-Behind to bind it to the ViewModel, which I would like to avoid.
So, I'm just wondering if there is a way to do this only with XAML
and MVVM.
Edit:
ItemSource attempt, which is working.
XAML:
<TabControl Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Stretch"
BorderThickness="0.5"
BorderBrush="Black"
ItemsSource="{Binding Model.TabItems, Mode=TwoWay}">
<!--<TabControl.Items>
</TabControl.Items>-->
</TabControl>
Model
public ObservableCollection<TabItem> TabItems {get;set;}
VM
TabItem tabItem = new TabItem { Content = new DetailedViewModel((MyObject)inCommandParameter) };
Model.TabItems.Add(tabItem);
XAML
is that you set all yourTabItems
but you set their Visibility tofalse
, and then when you need them you show them. And if you are usingItemSource
, well then I suggest you to check this. – arcticwhiteItemSource
its working, but I have some fixTabItem
's and others that I have to create dynamically, it makes sense for me to create onXAML
the fixed ones, and the others in the ViewModel, but seems that I will have to do "everything or nothing" style. Thanks for your answer! – NekeniehlItemsSource
attempt? – XAMlMAXXAML
and in the VM – NekeniehlCompositeCollection
andCollectionViewSource
? – XAMlMAX