WPF Dynamic TabItems
I have a TabControl with dynamically created tabs and content that vary depending on the XML that is passed in but, I also need a couple of TabItems to be created regardless of the XML content. When I add a <TabItem Header="Users"/>
I receive a Items Collection must be empty before using an ItemsSource. Here is what I have now:
<TabControl ItemsSource="{Binding Content}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="Header"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<TextBlock Text="Tab Content"/>
</DataTemplate开发者_开发知识库>
</TabControl.ContentTemplate>
</TabControl>
Is there anyway to do this? Thanks!
As indicated by the error, you must choose between either data binding the ItemsSource
, or setting the Items
property - you can't do both. You could use a CompositeCollection
to combine the contents of the dynamic tabs with another collection containing the non-dynamic tabs. You can then bind the ItemsSource
to this CompositeCollection
.
You can use the CompositeCollection to achieve the desired functionality. This might be helpful(though it talked TreeView here) - Binding to a single element inside a CompositeCollection
精彩评论