title: Passing value from child window to parent window using WPF and MVVM pattern
body: <p>I have parent window which has textBox called "SchoolName", and a button called "Lookup school Name".</p>
  
  <p>That Button opens a child window with list of school names. Now when user selects school Name from child window, and clicks on "Use selected school" button. I need to populate selected school in parent view's textbox.</p>
  
  <h2>Note: I have adopted Sam’s and other people’s suggestion to make this code work. I have updated my code so other people can simply use it.</h2>
  
  <h2>SelectSchoolView.xaml (Parent Window)</h2>
  
  <pre><code>&lt;Window x:Class="MyProject.UI.SelectSchoolView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Parent" Height="202" Width="547"&gt;
  &lt;Grid&gt;
      &lt;TextBox Height="23" Width="192" 
       Name="txtSchoolNames"  
       Text="{Binding Path=SchoolNames, UpdateSourceTrigger=PropertyChanged, 
       Mode=TwoWay}" 
       /&gt;
  
      &lt;Label Content="School Codes" Height="28" HorizontalAlignment="Left" 
       Margin="30,38,0,0" Name="label1" VerticalAlignment="Top" /&gt;
      &lt;Button Content="Lookup School Code" Height="30" HorizontalAlignment="Left" 
       Margin="321,36,0,0" Name="button1" VerticalAlignment="Top" Width="163" 
       Command="{Binding Path=DisplayLookupDialogCommand}"/&gt;
  &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <h2>SchoolNameLookup.xaml (Child Window for Look up School Name)</h2>
  
  <pre><code>&lt;Window x:Class="MyProject.UI.SchoolNameLookup"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
      Title="SchoolCodeLookup" Height="335" Width="426"&gt;
  
  &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="226*" /&gt;
          &lt;RowDefinition Height="70*" /&gt;
      &lt;/Grid.RowDefinitions&gt;
  
      &lt;toolkit:DataGrid Grid.Row="0" Grid.Column="1"   x:Name="dgSchoolList" 
                            ItemsSource="{Binding Path=SchoolList}" 
                            SelectedItem="{Binding Path=SelectedSchoolItem, Mode=TwoWay}" 
                            Width="294"
                            AutoGenerateColumns="False"
                            CanUserAddRows="False" 
                            CanUserDeleteRows="False"
                            CanUserResizeRows="False" 
                            CanUserSortColumns="True" 
                            SelectionMode="Single"&gt;
  
          &lt;Button Grid.Row="1" Grid.Column="1" Content="Use Selected School Name" 
           Height="23" Name="btnSelect" Width="131" Command="{Binding 
           Path=UseSelectedSchoolNameCommand}"  /&gt;
   &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <h2>SchoolNameLookupViewModel</h2>
  
  <pre><code>   private string _schoolNames;
     public string SchoolNames
     {
          get { return _schoolNames; }
          set
          {
              _schoolNames= value;
              OnPropertyChanged(SchoolNames);
          }
     }
  
     private ICommand _useSelectedSchoolNameCommand;
     public ICommand UseSelectedSchoolNameCommand{
     get
      {
      if (_useSelectedSchoolNameCommand== null)
          _useSelectedSchoolNameCommand= new RelayCommand(a =&gt; 
              DoUseSelectedSchollNameItem(), p =&gt; true);
              return _useSelectedSchoolNameCommand;
        }
      set
         {
           _useSelectedSchoolNameCommand= value;
         }
  
      }
  
      private void DoUseSelectedSchoolNameItem() {
          StringBuilder sfiString = new StringBuilder();
          ObservableCollection&lt;SchoolModel&gt; oCol = 
                  new ObservableCollection&lt;SchoolModel&gt;();
          foreach (SchoolModel itm in SchollNameList)
          {
              if (itm.isSelected) {
                  sfiString.Append(itm.SchoolName + "; ");
                  _schoolNames = sfiString.ToString();
              }
          }
                  OnPropertyChanged(SchoolNames);
      }
  
      private ICommand _displayLookupDialogCommand;
      public ICommand DisplayLookupDialogCommand
      {
          get
          {
              if (_displayLookupDialogCommand== null)
                  _displayLookupDialogCommand= new
                      RelayCommand(a =&gt; DoDisplayLookupDialog(), p =&gt; true);
              return _displayLookupDialogCommand;
          }
          set
          {
              _displayLookupDialogCommand= value;
          }
      }
  
      private void DoDisplayLookupDialog()
      {
          SchoolNameLookup snl = new SchoolNameLookup();
          snl.DataContext = this; //==&gt; This what I was missing. Now my code works as I was expecting
          snl.Show();
      }
  </code></pre>