N
N
Nikita2017-05-22 13:37:12
.NET
Nikita, 2017-05-22 13:37:12

How to pass value from one ViewModel to another?

The essence of the problem is this. There is a main window in which ListBox lies (List of lines is stored in it). Next to the leaf box are 3 buttons (add, delete and edit). There is a child window in which a line is created or edited (which was selected in the sheet box). How do I pass from one ViewModel to my other Row?
Of course, there is an idea when executing the Command to add or edit, create a ViewModel of a child window, transfer it to the required line, then create a child View and transfer the previously created ViewModel to the DataContex. But it seems to me that this approach is not quite correct for MVVW.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SitrixEug, 2017-05-29
@Smiz001

The main essence of MVVM - ViewModel knows nothing about the interface (View). View knows about ViewModel. Therefore, it is certainly wrong to create a specific View in the command handler.
But view can be associated with ViewModel in xaml code, which is already part of the interface, something like this:

<DataTemplate DataType="{x:Type vm:TrollViewModel}">
    <v:TrollView />
  </DataTemplate>

And in the command handler, only create a ViewModel. More or less like this:
protected void ShowWorkspace<T>() where T : WorkspaceViewModel, new()
        {
            WorkspaceViewModel workspace = this.Workspaces.FirstOrDefault(vm => vm is T) as T;

            if (workspace == null)
            {
                workspace = new T();
                this.Workspaces.Add(workspace);
            }

            this.SetActiveWorkspace(workspace);
        }

        protected void SetActiveWorkspace(WorkspaceViewModel workspace)
        {
            Debug.Assert(this.Workspaces.Contains(workspace));

            ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.Workspaces);
            if (collectionView != null)
                collectionView.MoveCurrentTo(workspace);
        }

More about this decision in the author's
article. A line can be passed as an object. Your ListView is bound to some Collection. Pass this SomeObject as a parameter to the ViewModel. The main thing is that it inherits the INotifyPropertyChanged interface.

D
Dmitry Bashinsky, 2017-11-05
@BashkaMen

I'm not sure if my method is correct, but I did this:
When the edit button is pressed, I initialize the viemodel and the form, then I subscribe to the editing completion event, for example, the OK button on that form, I throw the viewmodel into the datacontext of that form and show it. It turns out when that form completes editing, then you will receive an event that, for example, will replace the value of the selected element.
There is also an option, as for me it’s better and easier, make the user control have a textbox and a textblock, by default the textbox (it’s on top) is hidden, when the textblock catches the double click event, change the visibility of the textbox, it will make it possible to edit the text, but then I think it’s clear: )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question