I hope this is an easy question. (Been coding for years, but limited WPF experience.) I have a simple tab control.
<TabControl Name="TabControl">
That contains three tabs:
<TabItem Name ="View" Header="View">
stuff here to view data
</TabItem>
<TabItem Name ="Add" Header="Add">
a form to add data
</TabItem>
<TabItem Name ="Edit" Header="Edit">
the same form to edit data
</TabItem>
Since I want Add/Edit to share the same controls I created a ContentTemplate per this answer.
<TabControl.ContentTemplate>
<DataTemplate>
stuff in here
</DataTemplate>
</TabControl.ContentTemplate>
However, ALL my tabs share the DataTemplate. How to I hide/exclude the View tab from sharing these controls?
Usually, you have a distinct view model type for each type of data that you want to display in an
ItemsControl. Each of these view models would be displayed using aDataTemplatethat defines their appearance, e.g.:ViewDataItemDataYou would create data templates for each of these view model or data types for your
TabControl.By only specifying a
DataTypethe templates will automatically be applied to the corresponding item types inItemsSource. This also works, if you addTabItemsmanually and set the data items as content, e.g.:As an alternative to the implicit data templates, you can create a custom
DataTemplateSelectorand overrideSelectTemplate. This way, you can select data templates not only depending on the data type, but also individual properties, fields, methods or attributes, whatever you need.In XAML, you would create data templates with a key and add the data template selector.
Again this works both for an
ItemsSourceand manually added data items in aTabItem.