I have a MainWindow that contains a window with a TreeView. The tree view binds to an observable collection that I set in the DataContext.
<TreeView ItemsSource="{Binding Trees}" Name="fileTree" MouseDoubleClick="FileTreeMouseDoubleClick" SelectedValuePath="NodePath">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:TreeNodeViewModel}" ItemsSource="{Binding Children}">
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
However, I want to put a separate tree as a child of the MainWindow, which will bind to a different object, how can I do that If I have already used the DataContext property of the MainWindow.xaml?
EDIT: Extension to Question
Now I have:
<TreeView Name="viewTree" ItemsSource="{Binding ViewListTrees, Source=viewListTreeViewModel}">
Where viewListTreeViewModel is a member variable in MainWindow.xaml.cs:
private ViewListTreeViewModel viewListTreeViewModel;
which has the following accessor:
public ObservableCollection<ViewListTreeNodeViewModel> ViewListTrees
{
get { return this.tree; }
}
and ViewListTreeNodeViewModel has:
public string NodeName { get; }
public string NodeImage { get; }
My hierarchical data template now looks like:
<HierarchicalDataTemplate DataType="{x:Type local:ViewListTreeNodeViewModel}" ItemsSource="{Binding Children}">
<StackPanel>
<Image Source="{Binding NodeImage}" />
<TextBlock Text="{Binding NodeName}"/>
</StackPanel>
</HierarchicalDataTemplate>
Simply expose two Properties on a class you bind to your Window (rather than binding the collection directly), exposing
ObservableCollectionproperties;TreesandSeperateTreeand bind each TreeView accordingly: