How can I create a user interface on the fly in my model?

I have fully used the MVVM template for our silverlight application. However, some of our user interfaces are data driven. Basically two subjects ...

  • Menu. We use Infragistics XamMenu.

  • We have a "panel" that allows users to add "snap-ins". This is similar to a portal site such as iGoogle.

In both cases, the user interface must be embedded at run time. I am currently running code in code because I do not see an easy way to access the user interface tree in the ViewModel.

To run the code in the view, I created an event in the ViewModel that fires after the data is loaded. So, I have to avoid the viewmodel link in the view code behind. I don’t like it, it is very ugly ... so basically two questions:

  • How can I get the view from the view model that the data is loaded without a direct link to the viewmodel in the view code? I am currently pulling a link from a data context.

  • Is it possible to create a user interface in a view model and use data binding. I was wondering if I can associate the “content” contentcontrol with some type (not sure which type will be) in the viewmodel? Of course, the bad part about this is that the ability to test the view model seems to go away. Is it possible to use binding in this way?

+3
source share
1 answer

To answer question 1, why aren't you using the MVVM light class "Messenger".

In your opinion, you register to listen to the message as follows:

Messenger.Default.Register<bool>(this, "MessageId", DoSomething);

Where DoSomething is a method that takes a boolean parameter (for example).

Then, to send a message from your view model, follow these steps:

 Messenger.Default.Send(false, "MessageId");

Hope this helps :) You need to add this to your needs:

using GalaSoft.MvvmLight.Messaging;
+2
source

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


All Articles