title: How to get datagrid cell object form its template field wpf
body: <p>Below is the sample code which I am using. I have created attached property for <code>TextBox</code> called <code>ErrorMessageServce.ErrorMessage</code>. Whenever the <code>ValidationError</code> was populated it will call the property-change event of <code>ErrorMessageService</code>.</p>
  
  <p>From there what I want is, I want to highlight that particular cell if there was an error. so i thought to do in <code>ErrorMessageServicePropertyChanged</code>, but there I am getting <code>TextBox</code> object.</p>
  
  <p>So question is: </p>
  
  <p>1) How to get Datagridcell from that textbox object;</p>
  
  <p>or:</p>
  
  <p>2) How to higlight that particular cell;</p>
  
  <p>3) How to display that particular cell in edit mode(i.e.. Textbox should be displayed)</p>
  
  <p><strong>XAML:</strong></p>
  
  <pre><code>&lt;DataGridTemplateColumn&gt;
      &lt;DataGridTemplateColumn.CellTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;TextBlock Grid.Column="1" 
                  Width="150" Height="25"&gt;
              &lt;/TextBlock&gt;
          &lt;/DataTemplate&gt;
      &lt;/DataGridTemplateColumn.CellTemplate&gt;
  
      &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;TextBox Grid.Column="1" Style="{DynamicResource ValidatingTextBox}"  
                  x:Name="NameText" Text="{Binding CompanyName,ValidatesOnDataErrors=True,ValidatesOnExceptions=True}" App:ErrorMessageService.ErrorMessage="{Binding ValidationResult,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="25"&gt;
              &lt;/TextBox&gt;
          &lt;/DataTemplate&gt;
      &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
  &lt;/DataGridTemplateColumn&gt;
  </code></pre>
  
  <p><code>ErrorMessageService</code></p>
  
  <pre><code>public static class ErrorMessageService
  {       
      public static readonly DependencyProperty ValidationErrorProperty =
          DependencyProperty.RegisterAttached("ErrorMessage", typeof(ValidationResult), typeof(ErrorMessageService),
         new FrameworkPropertyMetadata(default(ValidationResult), ErrorMessageServicePropertyChanged));
  
      public static ValidationResult GetErrorMessage(Control control)
      {
          return (ValidationResult)control.GetValue(ValidationErrorProperty);
      }
  
      public static void SetErrorMessage(Control control, object value)
      {
          control.SetValue(ValidationErrorProperty, value);
      }      
  
      private static void ErrorMessageServicePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
         //here i am getting d as textbox , from this how to get datagridcell object , so that i can highlight
      }
  }
  </code></pre>
  
  <p>Thanks.</p>