title: Custom WPF DataGrid Column
body: <p>im trying for the past week or so to make custom columns for the wpf datagrid</p>
  
  <p>the main point is that there are certain types of data that i display in multiple datagrids across the app, and i dont want to redeclare all the attributes everytime</p>
  
  <p>heres a sample of some code thats used again and again</p>
  
  <pre><code>&lt;DataGridComboBoxColumn SelectedValueBinding="{Binding PersonID}" SelectedValuePath="PersonID" DisplayMemberPath="PersonName" Header="Customer" ItemsSource="{Binding Source={StaticResource PersonSource}}" /&gt;
  </code></pre>
  
  <p>so i tried making a usercontrol like this</p>
  
  <pre><code>Public Class PersonColumn
  Inherits DataGridComboBoxColumn
  Sub New()
      SelectedValuePath = "PersonID"
      DisplayMemberPath = "PersonName"
      Header = "Customer"
  End Sub
  End Class
  </code></pre>
  
  <p>this is a help, but not complete , because </p>
  
  <ol>
  <li>i cant do any styling or other markup work</li>
  <li>i cant seem to use the <code>FindResource</code> command to get the PersonSource</li>
  </ol>
  
  <p>how can this be improved? all my searches show xaml redone again and again, this seems very redundant</p>
  
  <p>at the same time im trying to make a datepicker column but cant find any sample of a <strong>reusable</strong> column, just redefining in each datagrid again</p>
  
  <p>thank you very much</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>im now trying a totally different direction</p>
  
  <p>i made a usercontrol with the following markup</p>
  
  <pre><code>&lt;DataGridTemplateColumn Header="Date" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      xmlns:my="clr-namespace:SampleWpf"  x:Class="DateColumn"&gt;
  &lt;DataGridTemplateColumn.CellTemplate&gt;
      &lt;DataTemplate&gt;
          &lt;TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:DateColumn}}, Path=Value}" /&gt;
      &lt;/DataTemplate&gt;
  &lt;/DataGridTemplateColumn.CellTemplate&gt;
  &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
      &lt;DataTemplate&gt;
          &lt;DatePicker SelectedDate="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:DateColumn}}, Path=Value}"  /&gt;
      &lt;/DataTemplate&gt;
  &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
  </code></pre>
  
  <p></p>
  
  <p>and a bit of codebehind</p>
  
  <pre><code>Public Class DateColumn
  Inherits DataGridTemplateColumn
  Public Shared ReadOnly ValueProperty = DependencyProperty.Register("Value", GetType(Date?), GetType(DateColumn))
  Property Value As Date?
  End Class
  </code></pre>
  
  <p>but this is not working. the date is not showing up in the column, neither are my changes saved back to the datasource</p>
  
  <p>the datagrid xaml is dead simple</p>
  
  <pre><code>                &lt;my:DateColumn   Value="{Binding Path=ReceivedOn}" Header="ReceivedOn" /&gt;
  </code></pre>
  
  <p>how are you all doing these stuff?
  thanks a billion!</p>