Master-Detail With CollectionViewModel


Implementing a Master-Detail style UI with the CollectionViewModel I described in my previous post is pretty easy. With a selector control bound to a CollectionViewModel as shown in my previous post, you just need to add a content control to the View and bind its Content property to the current item and define how you want it to be rendered by defining a data template.

For the sample in my previous post the view’s XAML contains this:

<ContentControl Content="{Binding Path=Catalog.CurrentItem}">
    <ContentControl.ContentTemplate>
        <DataTemplate>
            ...
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>

Note: In WPF, you strictly only need to bind the content control’s Content property to the Catalog – WPF automatically binds to the Catalog’s current item for you. Not sure why they did it this way. I think an explicit binding to the CurrentItem property is easier to understand, and I don’t mind typing the extra characters. Sometimes a little more is definitely more 🙂

You can run the updated sample here and download the updated source here. I also updated it to use the final release assemblies from Prism 2.0, and fixed a glitch in the Silverlight Unit Testing assembly references…

Comments (3)

  1. xamlcoder says:

    Hey David,

    This looks like a good implementation, thanks for sharing it (I’ve already put it to use in our Composite Silverlight app).  I believe I found a bug in the CollectionViewModel.  The Refresh() method does not entirely recreate the view, so if you’ve defined a Filter it does not run again.

    A fairly easy fix on line 139:

    //UpdateCurrentIndex(-1);

    UpdateView();

    Regards,

    Joe

  2. Thank you for submitting this cool story – Trackback from DotNetShoutout

  3. dphill says:

    Hey Joe – Doh! You’re right. Many thanks for tracking this down. I added your fix to the CollectionModelView class.

    David.