title: C# WPF DataGrid Converters
body: <p>I've been trying to format fields in a datagrid for days now. How can I simply change the Period is a date field from access. In this attempt I keep getting the error:</p>
  
  <p><i>'{local:DateConverter}' value is not a valid MarkupExtension expression. Cannot resolve 'DateConverter' in namespace 'clr-namespace:Yabba'. 'DateConverter' must be a subclass of MarkupExtension.</i></p>
  
  <p>However the examples I was working from all show <i>DateConverter : IValueConverter</i>.</p>
  
  <p>I'm pretty much just want to change the column to list whatever I want based on the date. But can't get any 1 example/method to work.</p>
  
  <p>XAML</p>
  
  <pre><code>&lt;Window Name="MainForm" x:Class="Yabba.MainWindow"
      xmlns:local="clr-namespace:Yabba"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="655.217" Width="887.851" Loaded="Window_Loaded"&gt;
  &lt;Window.Resources&gt;
      &lt;local:DateConverter x:Key="dateConverter"/&gt;
  &lt;/Window.Resources&gt;
  &lt;Grid&gt;
      &lt;DataGrid Name="dataGrid1"  AutoGenerateColumns="False" PreviewKeyDown="dataGrid1_KeyDown" CanUserAddRows="false" SelectionUnit="FullRow" IsReadOnly="True" SelectionMode="Single" HorizontalAlignment="Left" VerticalAlignment="Top" Height="348" Width="753" SelectionChanged="dataGrid1_SelectionChanged" Margin="0,20,0,0" MouseDoubleClick="dataGrid1_MouseDoubleClick"&gt;
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Header="Question" Binding="{Binding title}"&gt;&lt;/DataGridTextColumn&gt;
              &lt;DataGridTextColumn Header="Period" Binding="{Binding started, Converter={local:DateConverter}}"&gt;&lt;/DataGridTextColumn&gt;
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>Code</p>
  
  <pre><code>namespace Yabba {
  /// &lt;summary&gt;
  [ValueConversion(typeof(DateTime), typeof(String))]
  public class DateConverter : IValueConverter {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
          DateTime date = (DateTime)value;
          return date.ToShortDateString();
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
          string strValue = value as string;
          DateTime resultDateTime;
          if (DateTime.TryParse(strValue, out resultDateTime)) {
              return resultDateTime;
          }
          return DependencyProperty.UnsetValue;
      }
  }
  </code></pre>
  
  <p>What am I doing wrong here?</p>
  
  <p><b>Added notes to anyone using this as an example: (Unrelated to question, view selected answer for answer)</b></p>
  
  <p>You may need to change the types depending.</p>
  
  <pre><code>[ValueConversion(typeof(DateTime), typeof(String))]
  </code></pre>
  
  <p>I had to change mine to </p>
  
  <pre><code>[ValueConversion(typeof(String), typeof(String))]
  </code></pre>
  
  <p>Then recast to DateTime</p>
  
  <pre><code>DateTime date = DateTime.Parse((string)value);
  </code></pre>