I am using DatePicker control in my view with custom date format "dd/MM/yyyy". It's working fine when user input the date using calendar button. But when user type date in TextBox of DatePicker, it gives validation error. E.g: Suppose user wants to type date 25-Apr-2019. He types "25-04-2019", Textbox give validation error and gets red borders. But if user types "04-25-2019" it accepts.

I am using below xaml for DatePiker:

<DatePicker Grid.Column="1" Grid.Row="2" Name="dpEventDate" SelectedDate="{Binding Path=DateOfEvent, Mode=TwoWay, StringFormat = {}{0:dd/MM/yyyy}}"/>

And this style in Page.Resources:

        <Style TargetType="DatePickerTextBox">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBox Margin="0,2,0,0"
                            Text="{Binding Path=SelectedDate, StringFormat = {}{0:dd/MM/yyyy}, 
                            RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

How to fix this issue?

0 Answers