title: Editing currently selected item in datagrid
body: <p>Hei,</p>
  
  <p>I'm creating simple application with MVVM and stumbled on a problem which i find hard to resolve. On my application i have datagrid and couple of controls to edit currently selected item in datagrid. In my ViewModel i have <code>CurrentSequence</code> property what holds <code>ColorSettingsSequencesSequence</code> object (collection of these objects are used as DataContext for datagrid).</p>
  
  <p>Here's xaml:</p>
  
  <pre><code>&lt;DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=ColorSettingsSequences}"
                    SelectedItem="{Binding Path=CurrentSequence, Mode=TwoWay}"&gt;
      .... more things here ...
  &lt;/DataGrid&gt;
  
  &lt;StackPanel Grid.Column="0" Grid.Row="0"&gt;
      &lt;Grid&gt;
          &lt;Label Content="Start temperature (°C)" Height="28" HorizontalAlignment="Left" x:Name="lblSeqStartTemp" VerticalAlignment="Top" /&gt;
          &lt;TextBox Height="23" Margin="0,28,10,0" x:Name="tbSeqStartTemp" VerticalAlignment="Top" Text="{Binding Path=CurrentSequence.StartTemp}" /&gt;
      &lt;/Grid&gt;
      &lt;Grid&gt;
          &lt;Label Content="Start color" Height="28" HorizontalAlignment="Left" x:Name="lblSeqHue" VerticalAlignment="Top" /&gt;
          &lt;xctk:ColorPicker Margin="0,28,10,0" x:Name="clrpSeqHue" SelectedColor="{Binding Path=CurrentSequence.StartHue, Converter={StaticResource hueToColor}, ConverterParameter=False}" ShowStandardColors="False" /&gt;
      &lt;/Grid&gt;
  &lt;/StackPanel&gt;
  &lt;StackPanel Grid.Column="1" Grid.Row="0"&gt;
      &lt;Grid&gt;
          &lt;Label Content="End temperature (°C)" Height="28" HorizontalAlignment="Left" x:Name="lblSeqEndTemp" VerticalAlignment="Top" /&gt;
          &lt;TextBox Height="23" Margin="0,28,10,0" x:Name="tbSeqEndTemp" VerticalAlignment="Top" Text="{Binding Path=CurrentSequence.EndTemp}" /&gt;
      &lt;/Grid&gt;
      &lt;Grid&gt;
          &lt;Label Content="End color" Height="28" HorizontalAlignment="Left" x:Name="lblSeqEndHue" VerticalAlignment="Top" /&gt;
          &lt;xctk:ColorPicker Margin="0,28,10,0" x:Name="clrpSeqEndHue" SelectedColor="{Binding Path=CurrentSequence.EndHue, Converter={StaticResource hueToColor}, ConverterParameter=False}" ShowStandardColors="False" /&gt;
      &lt;/Grid&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>private ColorSettingsSequencesSequence _currentSequence;
  public ColorSettingsSequencesSequence CurrentSequence
  {
      get
      {
          return this._currentSequence;
      }
      set
      {
          this._currentSequence = value;
          OnPropertyChanged("CurrentSequence");
      }
  }
  </code></pre>
  
  <p>That works nicely, but the problem comes when i want to add validation. I would like to validate <code>StartTemp</code> and <code>EndTemp</code> separately and give different errors. How would i break up the <code>ColorSettingsSequencesSequence</code> object so that the bindings would also still work eq if i edit one value it gets updated in the datagrid also?</p>
  
  <p>Here's what i tried, i created 2 new properties and added my validation to those:</p>
  
  <pre><code>private String _currentSequenceStartTemp;
  public String CurrentSequenceStartTemp
  {
      get
      {
          return _currentSequenceStartTemp;
      }
      set
      {
          this._currentSequenceStartTemp = value;
          CurrentSequence.StartTemp = value;
          RaisePropertyChanged("CurrentSequenceStartTemp");
          Validator.Validate(() =&gt; CurrentSequenceStartTemp);
          ValidateCommand.Execute(null);
      }
  }
  
  private String _currentSequenceEndTemp;
  public String CurrentSequenceEndTemp
  {
      get
      {
          return _currentSequenceEndTemp;
      }
      set
      {
          this._currentSequenceEndTemp = value;
          CurrentSequence.EndTemp = value;
          RaisePropertyChanged("CurrentSequenceEndTemp");
          Validator.Validate(() =&gt; CurrentSequenceEndTemp);
          ValidateCommand.Execute(null);
      }
  }
  </code></pre>
  
  <p>And the i just binded TextBoxes to those values, instead of binding them straight to <code>CurrentSequence</code>. I also added setting the CurrentSequence values in the setters and hoped that way my changes will be pushed all the way back to original collection and will be changed in datagrid. That didn't happen..
  When CurrentSequence is changed i change values of these properties also:</p>
  
  <pre><code>private ColorSettingsSequencesSequence _currentSequence;
  public ColorSettingsSequencesSequence CurrentSequence
  {
      get
      {
          return this._currentSequence;
      }
      set
      {
          this._currentSequence = value;
          RaisePropertyChanged("CurrentSequence");
          if (value != null)
          {
              CurrentSequenceStartTemp = value.StartTemp;
              CurrentSequenceEndTemp = value.EndTemp;
          }
          else
          {
              CurrentSequenceStartTemp = String.Empty;
              CurrentSequenceEndTemp = String.Empty;
          }
      }
  }
  </code></pre>