WPF MVVM Master Detail View with TreeView Control
<p>I would like to implement a master/detail view (very much like the Windows Explorer) using a WPF TreeView control and the MVVM design pattern. I am safe regarding handling the TreeView control, but I do not know how I can pass the currently selected item in the master view (the TreeView) as DataContext to the Detail view (which is actually a UserControl Holding a ListView). Most preferably I would like to do that XAML. Does anybody know how to do that?</p>

<p>To clarify I'll post a little address book demo here.</p>

<p>This is the main window view which basically splits the client area in three columns using a grid. The left one holds the tree, the middle one a splitter and the right one the details view.</p>

<pre><code>&lt;Window.Resources&gt;
    &lt;HierarchicalDataTemplate DataType="{x:Type vm:Address}" ItemsSource="{Binding Residents}"&gt;
        &lt;TextBlock&gt;
            &lt;TextBlock.Text&gt;
                &lt;MultiBinding StringFormat="{}{0}, {1}, {2}"&gt;
                    &lt;Binding Path="Street"/&gt;
                    &lt;Binding Path="City"/&gt;
                    &lt;Binding Path="ZipCode"/&gt;
                &lt;/MultiBinding&gt;
            &lt;/TextBlock.Text&gt;
        &lt;/TextBlock&gt;
    &lt;/HierarchicalDataTemplate&gt;

    &lt;DataTemplate DataType="{x:Type vm:Resident}"&gt;
        &lt;TextBlock&gt;
            &lt;TextBlock.Text&gt;
                &lt;MultiBinding StringFormat="{}{0} {1}"&gt;
                    &lt;Binding Path="FirstName"/&gt;
                    &lt;Binding Path="LastName"/&gt;
                &lt;/MultiBinding&gt;
            &lt;/TextBlock.Text&gt;
        &lt;/TextBlock&gt;
    &lt;/DataTemplate&gt;
&lt;/Window.Resources&gt;

&lt;Grid&gt;
    &lt;Grid.ColumnDefinitions&gt;
        &lt;ColumnDefinition Width="150"/&gt;
        &lt;ColumnDefinition Width="5"/&gt;
        &lt;ColumnDefinition Width="*"/&gt;
    &lt;/Grid.ColumnDefinitions&gt;
    &lt;TreeView BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Addresses}"/&gt;
    &lt;GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch" /&gt;
    &lt;v:ResidentDetailView Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DataContext="???"/&gt;
&lt;/Grid&gt;
</code></pre>

<p>As you can see in the last line I would like to pass the selected item in the tree as DataContext to the Details view.</p>

<p>This is the details view which is a UserControl:</p>

<pre><code>&lt;ListView ItemsSource="{Binding Residents}"&gt;
    &lt;ListView.View&gt;
        &lt;GridView&gt;
            &lt;GridViewColumn DisplayMemberBinding="{Binding FirstName}" Header="FirstName" Width="100"/&gt;
            &lt;GridViewColumn DisplayMemberBinding="{Binding LastName}" Header="LastName" Width="100"/&gt;
        &lt;/GridView&gt;
    &lt;/ListView.View&gt;
&lt;/ListView&gt;
</code></pre>

<p>And finally the main window view model which I would like to share between main and Detail view:</p>

<pre><code>public class Resident
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public int ZipCode { get; set; }
    public List&lt;Resident&gt; Residents { get; } = new List&lt;Resident&gt;();
}

public class MainWindowViewModel
{
    public ObservableCollection&lt;Address&gt; Addresses { get; } = new ObservableCollection&lt;Address&gt;();

    public MainWindowViewModel()
    {
        var a = new Address {Street = "Broadway 1", City = "New York", ZipCode = 12345};
        a.Residents.Add(new Resident { FirstName = "John", LastName = "Miller" });
        a.Residents.Add(new Resident { FirstName = "Lisa", LastName = "Miller" });
        Addresses.Add(a);
        a = new Address { Street = "Wall Street 1", City = "New York", ZipCode = 12345 };
        a.Residents.Add(new Resident { FirstName = "Paul", LastName = "Walker" });
        a.Residents.Add(new Resident { FirstName = "Frank", LastName = "Brown" });
        a.Residents.Add(new Resident { FirstName = "Mark", LastName = "Smith" });
        Addresses.Add(a);
        a = new Address { Street = "Market Street 1", City = "San Francisco", ZipCode = 23456 };
        a.Residents.Add(new Resident { FirstName = "Jack", LastName = "Ness" });
        a.Residents.Add(new Resident { FirstName = "Joe", LastName = "Jackson" });
        a.Residents.Add(new Resident { FirstName = "Jill", LastName = "Baude" });
        Addresses.Add(a);
        a = new Address { Street = "Rodeo Drive 1", City = "Los Angeles", ZipCode = 34567 };
        a.Residents.Add(new Resident { FirstName = "Roger", LastName = "Water" });
        a.Residents.Add(new Resident { FirstName = "Andy", LastName = "Murray" });
        a.Residents.Add(new Resident { FirstName = "Peter", LastName = "Hammer" });
        a.Residents.Add(new Resident { FirstName = "Lola", LastName = "White" });
        Addresses.Add(a);
    }
}
</code></pre>

<p>Thanks for your help.
Johannes</p>