-2

The StringFormat shows correctly in the view as - dd/MM/yyyy , but when the user changes the DateTime the code refers to a date of this form MM/dd/yyyy and after the ENTER the StringFormat shows correctly in the view as - dd/MM/yyyy again .
The type of MnfDate and ExpDate is DateTime.

I tried to add mode = 2 but it didn't help

enter image description here

enter image description here

LopDev
  • 823
  • 10
  • 26
S_K
  • 53
  • 7
  • Looks like a ui culture issue. Take a look at the two approaches in the following link. Set the culture for everything near the entry point, or explicitly per binding. https://stackoverflow.com/questions/520115/stringformat-localization-issues-in-wpf/520334#520334 – Andy Sep 15 '20 at 09:20
  • 1
    ספיר קורן: Both dates are formatted as `dd/MM/yyyy` in your screenshot, aren't they? – mm8 Sep 15 '20 at 13:43
  • Yes, both of them. – S_K Sep 15 '20 at 13:51

1 Answers1

0

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>
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
S_K
  • 53
  • 7