Dynamically add views in Silverlight and MVVM

I am starting to rewrite my entire silverlight business application in the MVVM template; My first stop:

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

Now in this view, I want to have, for example, a tree structure control in which one node will be populated with another View2;

My question is: 1. How to do it? - I can not go through the AllData property, since it is loaded asynchronously ... - so I don’t know how many View2s points I need to insert - I don’t know how to do this from ViewModel1 :(

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

You can help me?

thank

+3
source share
1 answer

Sounds like you're trying to put together a Master / Detail view? Where MasterView contains TreeView of all detailed representations.

So ViewModels looks something like this:

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

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

If it is more or less accurate, then you have a pretty simple way ahead of yourself and what you need to do is to first select View or ViewModel.

The first time you select View, you can use the DataTemplate for the TreeView, which will control the TreeViews element.

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

Just make sure your view has a default constructor.

ViewModel TemplateSelector, View out, . , , , Detail.

. http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector

+1

Source: https://habr.com/ru/post/1745124/


All Articles