Answer the question
In order to leave comments, you need to log in
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
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>
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);
}
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 questionAsk a Question
731 491 924 answers to any question