title: Silverlight Data Grid twoway Data binding in code
body: <p>I have a DataGrid with two way binding and not sure why this does not work, any help would be appreciated.  </p>
  
  <p>I wanted to dynamically bind to the DataGrid using a twoway binding object.  </p>
  
  <p>I used the columns in XAML.  If I just set the 'ItemSource" property directly - it works but then the two binding doesn't work - if I change my source in code the Grid doesn't reflect that change.</p>
  
  <p>I created a simple sample to illustrate my setup </p>
  
  <p>Here is the XAML </p>
  
  <pre><code>&lt;UserControl x:Class="SilverlightApplication1.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      d:DesignHeight="356" d:DesignWidth="590" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"&gt;
  
      &lt;Grid x:Name="LayoutRoot" Background="White"&gt; 
          &lt;sdk:DataGrid AutoGenerateColumns="False" Height="136" HorizontalAlignment="Left" Margin="71,116,0,0" Name="MyGrid" VerticalAlignment="Top" Width="453" &gt;
              &lt;sdk:DataGrid.Columns&gt;
                  &lt;sdk:DataGridTextColumn Binding="{Binding Path=Label, Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Header="Selected" /&gt;
                  &lt;sdk:DataGridTextColumn Binding="{Binding Path=YValue, Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Name" Width="*" /&gt;
              &lt;/sdk:DataGrid.Columns&gt;
          &lt;/sdk:DataGrid&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>Here is the code behind</p>
  
  <pre><code>namespace SilverlightApplication1
  {
      public partial class MainPage : UserControl
      {
          public MainPage()
          {
              InitializeComponent(); 
              ObservableCollection&lt;Value&gt; values = new ObservableCollection&lt;Value&gt;(); 
              values.Add(new Value() { Label = "Sony", YValue = 50 });
              values.Add(new Value() { Label = "Dell", YValue = 35 });
              values.Add(new Value() { Label = "HP", YValue = 27 });
              values.Add(new Value() { Label = "HCL", YValue = 17 });
              values.Add(new Value() { Label = "Toshiba", YValue = 16 });
  
              PagedCollectionView p = new PagedCollectionView(values); 
  
              Binding b = new Binding("ValuesBinding");
              b.Mode = BindingMode.TwoWay;
              b.Source = values;
              MyGrid.SetBinding(DataGrid.ItemsSourceProperty, b);  
          }
      }
  
      public class Value : INotifyPropertyChanged
      {
          public String Label
          {
              get
              { return _label; }
              set
              {
                  _label = value;
                  if (PropertyChanged != null)
                      PropertyChanged(this, new PropertyChangedEventArgs("Label"));
              }
          }
           public Double YValue
          {
              get
              {return _yValue;}
              set
              {
                  _yValue = value;
                  if (PropertyChanged != null)
                      PropertyChanged(this, new PropertyChangedEventArgs("YValue"));
              }
          }
          #region INotifyPropertyChanged Members
          public event PropertyChangedEventHandler PropertyChanged;
          #endregion
          Double _yValue;
          String _label;
      } 
  
  }
  </code></pre>