Let ViewModel Know a Common View Object

I have a general view, where I "insert" a specific view into the contained ContentControl (I created this function using this help -> help 1 - help 2 ).

The main source of my views are the following:

MyGenericView.xaml

 <UserControl x:Class="MyNS.MyGenericView" ... > <UserControl.Resources> <vml:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> </UserControl.Resources> <Grid DataContext="{Binding MyGenericViewModel, Source={StaticResource Locator}}"> <ContentControl Content="{Binding MyObject}" /> </Grid> </UserControl> 

CustomerView.xaml

 <UserControl x:Class="AnotherNS.CustomerView" ... > <Grid> <StackPanel Orientation="Vertical"> <Label Content="Description" /> <TextBox Text="{Binding description}" /> </StackPanel> </Grid> </UserControl> 

Crud.xaml : a resource dictionary that I use to “solve” what shows the right kind, depending on the DataType of the MyObject provided by the general view.

 <ResourceDictionary ... > <DataTemplate DataType="{x:Type mo:Customer}"> <vw:CustomerView /> </DataTemplate> <DataTemplate DataType="{x:Type mo:Product}"> <vw:ProductView /> </DataTemplate> ... </ResourceDictionary> 

It is working fine. I can control MyObject through a "specific" view (client, product, etc.).

Well. What is my problem:

All specific views have their own ViewModels and, of course, they manage the data of the respective views. But I don’t know (on the viewmodel) which object (MyObject) I work with, because the Generic View provides it with a specific view, not a viewmodel.

Is there a way to let ViewModels of certain views recognize the object that “commands” the view?

0
source share
1 answer

In MVVM Light, you will send a broadcast message of the publisher / subscriber type, which will allow you to communicate with the hosting model without having a hard link to the hosting control in the hosted control.

This allows the hosted control to remain disconnected from the hosting control and still interact between them.

EDIT:

MVVM Light has an instant messenger object that takes care of a lot for you. You can create message classes that contain different messages and arguments that they send separately. You can also specify a “token” only a specific line (I usually configure a set of constants in the class to accommodate my different “tokens”), which allow only subscribers to send this message and this token to receive the message. I have included a sample code below what I use in MVVM Light in v3 MVVM Light, you need to unregister from the message, because it does not use the weak events pattern.

If you do not want to use MVVM Light, you can use the same idea in your publisher / subscriber model, you just need to do the work of determining whether the token sent by the publisher is the token that the subscriber is looking for

 public static class DescriptiveMessageName { public static void Send(object args) { Messenger.Default.Send(args, "SpecificToken"); } public static void Send(object args, string token) { Messenger.Default.Send(args, token); } public static void Register(object recipient, Action<object> action) { Messenger.Default.Register(recipient, "SpecificToken", action); } public static void Register(object recipient, string token, Action<object> action) { Messenger.Default.Register(recipient, token, action); } public static void UnRegister(object recipient) { Messenger.Default.Unregister<object>(recipient); } public static void UnRegister(object recipient, Action<object> action) { Messenger.Default.Unregister<object>(recipient, action); } 

}

+1
source

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


All Articles