3
votes

I am starting to re-write my whole silverlight business application in the MVVM pattern; My first stop-point is this:

I have a page (View1) with corresponding ViewModel1 (with a property 'IEnumerable AllData');

Now, within this View, I want to have i.e. a tree-view control, in which one node will be populated with another View2;

My question is: 1. How to do it? - I can't loop through the AllData property since it is asynchronously loaded... - thus I don't know the number of View2s' to insert - I don't know how to do it from ViewModel1 :(

  1. Will I need ViewModel2 with property 'MyDataEntity CurrentData'?
    • or I can bind to AllData property from ViewModel1

Can you help me out?

Thanks

1
Does ViewModel2 need to access AllData or only one specific item? - Stephan
All that ViewModel2 should need is AllData[index], which is an instance of MyDataEntity, needed to bind with View2 controls; The thing is, I need to somehow iterate through AllData and generate/append as many View2 controls as members in AllData IEnumerable - dvox

1 Answers

1
votes

Its sounds like you are trying to put together a Master/Detail view? Where the MasterView contains the TreeView of all the DetailViews.

So the ViewModels looks something like

public class DataListViewModel
{
    public DataDetailViewModel[] AllData {get;}
}

public class DataDetailViewModel
{
    public Data Model {get;}
    public DataListViewModel Parent {get;}
}

If this is more or less accurate then you have a pretty easy road ahead of yourself and what you need to do is make a choice of View Or ViewModel first.

With the View first choice you can use DataTemplate for the TreeView that will control the TreeViews Item.

<DataTemplate x:Key="dataTemplate">
  <my:DataDetailView DataContext="{Binding Path=.}" />
</DataTemplate>

Just make sure your View has a default constructor.

With a ViewModel first choice you will need to use a TemplateSelector that will pull the correct View out, based on the object being set. This is probably the most flexible, as you could use different Views based on the Detail class.

See the link for more information http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector