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.