title: WPF Datagrid sort on column with null elements
body: <p>I have a WPF Datagrid that I am using with a number of columns. One of the columns has some elements that are sometimes null and this causes an exception when I try and sort on this column.</p>
  
  <p>The definitions of the columns is something like:</p>
  
  <pre><code>&lt;dg:DataGrid.Columns&gt;
    &lt;dg:DataGridTextColumn Binding="{Binding MyObject.Field1}" Header="Field1" Width="Auto" /&gt;
    &lt;dg:DataGridTextColumn Binding="{Binding MyObject.Field2.SubField}" Header="Field2" Width="Auto" /&gt;
  &lt;/dg:DataGrid.Columns&gt;
  </code></pre>
  
  <p>If I sort on Field1 column it is fine, if I sort on Field2 column and there are no null Field2 objects it is fine, but sometimes there are and the DataGrid tries to sort on the SubField (I guess) and hits a null exception:</p>
  
  <pre><code>System.InvalidOperationException was unhandled
    Message=The SortDescriptions added are not valid. The probable solutions are to set the CanUserSort on the Column to false, or to use SortMemberPath property on the Column, or to handle the Sorting event on DataGrid.
  </code></pre>
  
  <p>I have tried setting SortMemberPath to "MyObject.Field2.SubField" but of course this doesn't fix it since Field2 is still sometimes null. I wondered about trying to use a converter where I set the SortMemberPath and have that converter return string.empty for any null elements but couldn't get it to work. </p>
  
  <p>I also tried adding "TargetNullValue={x:Static sys:String.Empty}" within the binding of these columns but it still did not work.</p>
  
  <p>Any advice/suggestions would be most appreciated.
  Thanks,
  Will</p>