title: Nested DataGrid Binding Value to Property
body: <p>I have a <code>DataGrid</code> that is bound to a <code>List</code> called HldList which is of type <code>Holding</code> (class is shown below as <code>Fund</code>).</p>
  
  <p>When one of the rows is selected the row details expands to show another <code>DataGrid</code> (lets call this my sub datagrid) this is bound to the <code>List</code> Funds. </p>
  
  <p>The bindings all work as expected.</p>
  
  <p>The code for my row details template is in my <code>app.xaml</code> file which is shown at the bottom of this post.</p>
  
  <p>The one thing I can't get to work is the following. In my row details <code>DataGrid</code> I also have a <code>TextBox</code> where a user can enter a value (a ratio in this case) I want to bind the value of this <code>TextBox</code> to the Ratio property defined in my Holding class but can't seem to get it to work</p>
  
  <p><strong>Classes</strong></p>
  
  <pre><code> class Holding : INotifyPropertyChanged
   {
          private string _code;
          public string Code
          {
              get
              {
                  return _code;
              }
              set
              {
                  _code = value;
                  OnPropertyChanged("Code");
              }
          }
  
          private string _ratio;
          public string Ratio
          {
              get
              {
                  return _ratio;
              }
              set
              {
                  _ratio = value;
                  OnPropertyChanged("Ratio");
              }
          }
  
          private List&lt;Fund&gt; _funds;
          public List&lt;Fund&gt; Funds
          {
              get
              {
                  return _funds;
              }
              set
              {
                  _funds = value;
                  OnPropertyChanged("Funds");
              }
          }
            public event PropertyChangedEventHandler PropertyChanged;
  
            void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                  PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
   }
  
  
   class Funds : INotifyPropertyChanged
   {
          private string _name;
          public string Name
          {
              get
              {
                  return _name;
              }
              set
              {
                  _name = value;
                  OnPropertyChanged("Name");
              }
          }
  
          private double _nominal;
          public double Nominal
          {
              get
              {
                  return _nominal;
              }
              set
              {
                  _nominal = value;
                  OnPropertyChanged("Nominal");
              }
          }
            public event PropertyChangedEventHandler PropertyChanged;
  
            void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                  PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
   }    
  </code></pre>
  
  <p><strong>App.xaml</strong></p>
  
  <pre><code> &lt;DataTemplate x:Key="DG_RowDetailRatio"&gt;
          &lt;Grid x:Name="RowDetailGrid"            
                Margin="5"
                HorizontalAlignment="Left"&gt;
              &lt;Border HorizontalAlignment="Left"
                      VerticalAlignment="Top"
                      Height="250"
                      CornerRadius="5"&gt;
                  &lt;Border.Background&gt;
                      &lt;LinearGradientBrush StartPoint="0,0" EndPoint="0,1"&gt;
                          &lt;GradientStop Offset="0" Color="Transparent"/&gt;
                          &lt;GradientStop Offset="1" Color="Transparent"/&gt;
                      &lt;/LinearGradientBrush&gt;
                  &lt;/Border.Background&gt;
                  &lt;Grid&gt;
                      &lt;Grid.RowDefinitions&gt;
                          &lt;RowDefinition Height="*"/&gt;
                          &lt;RowDefinition Height="4*"/&gt;
                      &lt;/Grid.RowDefinitions&gt;
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition Width="400"/&gt;
                          &lt;ColumnDefinition/&gt;
                          &lt;ColumnDefinition/&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
                      &lt;TextBlock Grid.Row="0"
                                 Grid.Column="0"
                                 Margin="5,5,5,5"
                                 HorizontalAlignment="Left"
                                 FontSize="12"
                                 FontWeight="Bold"
                                 Foreground="Black" 
                                 Text="Select funds to be updated"&gt;
                      &lt;/TextBlock&gt;
                      &lt;DataGrid Grid.Row="1" Grid.Column="0"  Grid.ColumnSpan="2"
                                ItemsSource="{Binding SelectedItem.Funds,  RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"                                                                  
                                RowStyle="{StaticResource DG_Row}"  
                                ColumnHeaderStyle="{StaticResource DG_ColumnHeader}" 
                                RowHeaderStyle="{StaticResource DG_RowHeaderNested}"
                                CellStyle="{StaticResource DG_Cell}" 
                                Background="Silver"
                                HorizontalGridLinesBrush="LightGray"
                                VerticalGridLinesBrush="LightGray"
                                CanUserAddRows="False"
                                CanUserDeleteRows="False"
                                Margin="50,5,5,20"
                                AutoGenerateColumns="False"&gt;
                          &lt;DataGrid.Columns&gt;
                              &lt;DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True" MinWidth="75"/&gt;
                              &lt;DataGridTextColumn Header="Nominal" Binding="{Binding Nominal}" IsReadOnly="True" MinWidth="75"/&gt;
                          &lt;/DataGrid.Columns&gt;
                      &lt;/DataGrid&gt;
                      &lt;Grid Grid.Row="1" Grid.Column="2"&gt;
                          &lt;Grid.ColumnDefinitions&gt;
                              &lt;ColumnDefinition/&gt;
                              &lt;ColumnDefinition/&gt;
                              &lt;ColumnDefinition/&gt;
                          &lt;/Grid.ColumnDefinitions&gt;
                          &lt;TextBlock Grid.Row="0" Grid.Column="0" Margin="50,5,0,0" HorizontalAlignment="Left" FontSize="12"
                                 FontWeight="Bold" Foreground="Black" Text="Or enter Ratio against acquired company nominals"&gt;
                          &lt;/TextBlock&gt;
                          &lt;CheckBox x:Name="chkRatio" Grid.Row="0" Grid.Column="1" Margin="20,5,0,0" Height="30" 
                                    IsChecked="{Binding UseRatio}" HorizontalAlignment="Left" VerticalAlignment="Top"/&gt;
                          &lt;TextBox Grid.Row="0" Grid.Column="2" Height="30" Width="50"  Margin="20,5,0,0" 
                                   ToolTip="Enter ratio" HorizontalAlignment="Left" VerticalAlignment="Top"
                                   Text="{Binding HldList.Ratio, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                   Visibility="{Binding IsChecked, ElementName=chkRatio, Converter={StaticResource BoolToVis}}"/&gt;
                      &lt;/Grid&gt;
                  &lt;/Grid&gt;
              &lt;/Border&gt;
          &lt;/Grid&gt;
      &lt;/DataTemplate&gt;
  </code></pre>