I am trying to access values from a user control MVVM in a parent MVVM class – so I can set the menu to enable/disable. I cannot visualize how to wire this up.
Any help is appreciated for the below since I have stuck (as I learn to implement MVVM)
Q1) As mentioned above – how do I access values from user control (child) MVVM onto the parent windowviewmodel
Q2) Once these values are accessible – I need to wire them with the CommandBinding of the menu (Edit) item (for enable/disable). How do I do that?
Q3) Minor question – currently in my loginviewmodel – only when I tab out does the Submit button get enabled – if I wanted to do it as soon as I type a character – how do I go about?
Code for my loginscreen.xaml.cs
public partial class Login_Screen_User_Control : UserControl
{
public Login_Screen_User_Control()
{
InitializeComponent();
var loginmodel = new myWindowViewModel();
DataContext = loginmodel;
}
}
public class myWindowViewModel
{
private string _username;
private string _password;
public ICommand OkCommand { get; set; }
public myWindowViewModel()
{
OkCommand = new myCommand(myOkExecute, myCanOkExecute);
}
public string userName
{
get { return _username; }
set { _username = value; }
}
public string passWord
{
get { return _password; }
set { _password = value; }
}
private void myOkExecute(object parameter)
{
//Authetication Logic - lets assume this came back as true
}
private bool myCanOkExecute(object parameter)
{
return (string.IsNullOrEmpty(userName) ? false : (userName.Length > 0 ? true : false));
}
}
Code for my ParentApp.xaml
<Window x:Class="Complete_Application.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:_05_User_Control_POC;assembly=05-User_Control_POC"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Menu Grid.Row="0" Name="menu1" Height="25">
<MenuItem Header="_File">
<MenuItem Header="_New"/>
<MenuItem Header="_Open"/>
<MenuItem Header="_Close"/>
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Header="_New"/>
<MenuItem Header="_Open"/>
<MenuItem Header="_Close"/>
</MenuItem>
</Menu>
<Grid Grid.Row="1">
<loc:Login_Screen_User_Control></loc:Login_Screen_User_Control>
</Grid>
</Grid>
Code for myparent.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var mainwindowviewmodel = new parentWindowViewModel();
DataContext = mainwindowviewmodel;
}
}
public class parentWindowViewModel
{
public parentWindowViewModel()
{
//??
}
}

I have looked at the following article – the person is doing the exact similar thing but through singleton pattern which I dont want to use – WPF MVVM – Simple login to an application
A1)
Option A: use a Mediator/Messenger to transfer the values (
mvvm light messenger)Option B: (recommended) have LoginVM as a child property of your parent and use
DataTemplateA2) I don’t completely understand via the wording, but
{Binding LoginChild.VALUE}A3) Binding.UpdateSourceTrigger