I’ve done this with list view in that I can define a resource:
<UserControl.Resources>
<GridView x:Key="MyGrid" x:Shared="False">
<!-- Defines what's in the grid view -->
</GridView>
</UserControl.Resources>
Then I can have two views using the same Grid:
<ListView View="{DynamicResource MyGrid}" ItemsSource="{Binding Path=TodaysItems}"/>
<ListView View="{DynamicResource MyGrid}" ItemsSource="{Binding Path=TomorrowsItems}"/>
I’m trying to do the same thing with TreeViews. I’ve defined my tree view:
<UserControl.Resources>
<TreeView x:Key="MyTreeView" x:Shared="False">
<!-- Defines what's in the Tree view -->
</TreeView>
</UserControl.Resources>
But I can’t find what I need to do
<TreeView ???="{DynamicResource MyTreeView}" ItemsSource="{Binding Path=ClientData}"/>
<TreeView ???="{DynamicResource MyTreeView}" ItemsSource="{Binding Path=CustomerData}"/>
Can I even do this?
You cannot do this with
TreeView. The reason why you can do it with theListViewis that it has a propertyViewthat can be set to different views. The view in this case is not a standalone UI element – it just, lets say, “settings” for theListView. While the TreeView is a UI element just likeListView.The command approach to re-use in XAML is Styles. You can define a style for you
TreeViewwhere you can define common properties and then apply it to as many elements as you like.Here is an example how you can define a style:
And here is how you apply it: