简体   繁体   中英

WPF DatePicker US AND UK date format issue

I have a DatePicker inside a datagrid in which the user can select a date like this

"07/04/2014" which is in dd/MM/yyyy, however, when saved to the backend, it appears in the database like 2014-07-04 00:00:00.000. (so in US)

I believe this is causing my problem in which when a user selects a date over the 12th day, the DatePicker becomes invalid and the user wont be able to save.

So for example if the user selects 13/04/2014 the datepicker becomes invalided as the properly thinks the format is in MM/dd/yyyy format (I believe)

How do I get around this? do I convert the DateTime property in the model to UK format, or do I need to set the format of the datepicker to something different?

Here is the XAML

                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <DatePicker Text="{Binding From,   UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <DatePicker SelectedDate="{x:Static System:DateTime.Now}"></DatePicker>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate> 

And the property in the Model

   public System.DateTime From
    {
        get
        {
            return m_From;
        }
        set
        {
           m_From = value;
           PropertyChanged(this, new PropertyChangedEventArgs(From));
        }
    }

Any help would be good.

Thanks

@GerogeHowarth helped me here,

I realised that the CellTemplate

<DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <DatePicker Text="{Binding From,   UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                </DataTemplate>

Was passing a string back to the model, I should have been passing a date back

<DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <DatePicker SelectedDate="{Binding From,   UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                </DataTemplate>

This has seemed to solved the problem.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM