title: How to reuse a custom datagrid column code in WPF?
body: <p>Using VS 2010 I plan to make several datagrid template columns, but they will all contain a textblock and I want to them to behave like a text column in terms of sorting, filtering, editing, etc.  (For example, a column that has a textblock and an image within a stackpanel, but behavior-wise it should really be all about the text.)</p>
  
  <p>When using a template column, I've learned that much of the functionality associated with a normal text cell must be redone.  For instance, in order to make the text editable, one must provide a cell editing template like:</p>
  
  <pre><code>&lt;DataGridTemplateColumn.CellEditingTemplate&gt;
      &lt;DataTemplate&gt;
          &lt;TextBox
          FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"
          Text="{Binding Path=SomeProperty, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"&gt;
              &lt;/TextBox&gt;
      &lt;/DataTemplate&gt;
  &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
  </code></pre>
  
  <p>(<em>And</em> you must also handle grid events in order to make sure it behaves like a normal textbox, like automatically beginning edit mode if the user presses a key down, or tabs to the cell, etc.)</p>
  
  <p><b>So my question is, what is the best way (if any) for me to avoid having to explicitly write this code (<em>as well as</em> the code for sorting, copying, filtering, etc like a textcell, for EVERY template column I create like this?</b>  I'm assuming it's bad practice to be reproducing half a page's worth of code for each column, where the only differences might the binded property name and a handful of visual alterations.</p>
  
  <p>I am beyond frustrated with this, and with WPF in general.  I've scoured the web, I've tried adorners, I've tried inheriting a datagrid column, I've tried defining user controls for the data templates, and everything seems to fail with some nasty "gotcha".  This is about the 3rd question I've asked regarding this with varying details, with minimal response.  It should <em>NOT</em> be this hard to figure out how to implement what are basically glorified text columns, without needing to reinvent the entire wheel every single time and in every single respect.  In my humble opinion at least.</p>