1

I have to launch some sort of child window in my main ViewModel and on this child window user will enter some text and i have to use this text in main viewmodel just after closing the child window.

I am able to have a popup of child window which i bind to it's child window view model

Main view model where i am launching child window is:

private void OpenLayoutNameWindow()
{
    LayOutName_VM chldWindow =new LayOutName_VM();
    chldWindow.Show();
    string layOutName = chldWindow.LayOutName;
    MessageBox.Show("Name is:"+ layOutName);  //this message box is popuped before i close the child window save button and its empty.
}

This is the view of child window :

<StackPanel>
    <Label Margin="0,5,0,0" HorizontalAlignment="Center">Please name your new Layout</Label>
    <TextBox Margin="0,5,0,0" Width="120" Height="20" HorizontalAlignment="Center" Text="{Binding LayOutName, Mode=TwoWay}"></TextBox>
    <Button Margin="0,5,0,0" Width="80" Height="20" HorizontalAlignment="Center" Command="{Binding CloseWindowCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"> Save</Button>
</StackPanel>

This is the code behind:

public partial class LayOutName : Window
{
    public LayOutName()
    {
        InitializeComponent();
        this.DataContext = new LayOutName_VM(); ;
    }       
}

This is child window view model:

class LayOutName_VM : ViewModelBase
{
    public ActionCommand<Window> CloseWindowCommand { get; private set; }

    public LayOutName_VM()
    {
        this.CloseWindowCommand = new ActionCommand<Window>(this.SaveLayOutName);
    }

    private string layOutName;
    public string LayOutName
    {
        get
        {
            return layOutName;
        }
        set
        {
            layOutName = value;
            RaisePropertyChanged("LayOutName");
        }
    }

    private void SaveLayOutName(Window wind)
    {
        wind.Close();
    }

    internal void Show()
    {
        LayOutName ly = new BrukerApp.LayOutName();
        ly.Show();
    }
}

How to get the text entered in child window to viewmodel class.

yan yankelevich
  • 885
  • 11
  • 25
stuck stuck
  • 35
  • 1
  • 8
  • You shouldn't have anything in your code behind if you're trying to use MVVM. I see you have a `ViewModelBase`, are you using one of the MVVM libraries? If so, I would look into your flavors implementation of the `Mediator` pattern or `EventAggregator` pattern. – Adam Vincent Jul 10 '17 at 12:28

2 Answers2

1

You could call ShowDialog() instead of Show() to block:

chldWindow.ShowDialog();

Then you won't get to the following line of code until the dialog window has been closed:

string layOutName = chldWindow.LayOutName;

Also note that you should use some kind of service to open the window instead of doing it directly from the view model if you are serious about following the MVVM pattern:

Opening new window in MVVM WPF

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks for the comment. Do you know if the MVVM pattern for the C# works for Visual Basic as well or not? could you direct me to a reference on how it should be done in the Visual basic? Thanks. – Ehsan Jan 03 '20 at 20:16
0

Opening dialogs from view models, and still being able to write unit test for these view models, is harder than it has to be.

I'm the author of a framework called MVVM Dialogs, and I've got a sample that you can run showing off exactly what you are looking for.

FantasticFiasco
  • 1,133
  • 1
  • 8
  • 11