title: Format Nullable DataTime in a WPF DataGrid
body: <p>In a WPF application, i display data in a datagrid.
  In this datagrid there is some DateTime value.</p>
  
  <p>Where DateTime are not getting set, it displays 01/01/0001, it's ok because DateTime are not nullable.</p>
  
  <p>In this case, i would like to display " - " .</p>
  
  <p>I think i need to use a converter.</p>
  
  <p><strong>I tried this :</strong></p>
  
  <p>using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Globalization;
      using System.Windows.Data;</p>
  
  <pre><code>namespace Phoenix_CRM
  {
      public class Converter
      {
      }
  
      public class DateConverter : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              if (value == null)
                  return "--/--/----";
  
              DateTime returnVal;
  
              if (DateTime.TryParse(value.ToString(), out returnVal))
              {
                  if (returnVal != DateTime.MinValue)
                      return returnVal;
                  else
                      return "--/--/----";
              }
              else
                  return "--/--/----";
          }
  
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              if (value == null)
                  return DateTime.MinValue;
  
              DateTime val;
              if (value.ToString() == "--/--/----")
                  return DateTime.MinValue;
  
              if (DateTime.TryParse(value.ToString(), out val))
                  return val;
              else
                  return DateTime.MinValue;
          }
  
      }
  
  }
  </code></pre>
  
  <p><strong>I added this on my XAML :</strong></p>
  
  <pre><code>xmlns:converter="clr-namespace:myNameSpace" 
  </code></pre>
  
  <p><strong>On after :</strong></p>
  
  <pre><code>&lt;DataGrid Grid.Column="0" Grid.Row="2"&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn Binding="{ Binding Path= dDate,StringFormat={}{0:dd/MM/yyyy HH:mm},Converter={converter:Converter.DateConverter}}" Header="  Date  " Width="Auto" IsReadOnly="True"  /&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>My code don't compile because of the </p>
  
  <pre><code>Converter={converter:Converter.DateConverter}}
  </code></pre>
  
  <p>Anyone could help me please ?</p>