I solved this issue by using a DateConverter
.
Here is the code:
[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;
}
}
And the related markup:
<DataGrid>
<DataGrid.Resources>
<local:DateConverter x:Key="conv"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="{lex:LocText ResourceIdentifierKey=MO.Packing.Wpf:Localization.MoStrings:BatchNumberPickerViewDisplayName}"
Binding="{Binding BatchNum , Mode=TwoWay}" IsReadOnly="False"/>
<DataGridTextColumn Header="{lex:LocText ResourceIdentifierKey=MO.Packing.Wpf:Localization.MoStrings:MnfDate}"
Binding="{Binding MnfDate ,Mode=TwoWay ,StringFormat='{}{0:dd/MM/yyyy}', Converter={StaticResource conv}}" IsReadOnly="False"/>
</DataGrid.Columns>
</DataGrid>