0

I'm doing a wpf application and I have two fields where I have to have the date like this: dd/MM/yyyy hh:mm:ss but when I lose focus from the element the date goes to MM/dd/yyyy hh:mm:ss. And This only happens when I change something in the date. If I change one second the month and day switch.

If I type in "03/06/2019 16:58:00" when it loses focus the date is "06/03/2019 16:58:00". And that changes in the POPUP.

The problem is not in the format, but in the order it displays.

My XAML is like this:

<DatePicker x:Name="Dp_DataInicio" Grid.Row="0" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,211,0,0" FontSize="16" Height="20" Width="230" IsTodayHighlighted="False" BorderThickness="0" Padding="0" BorderBrush="{x:Null}" IsTabStop="True" Visibility="Hidden" SelectedDateChanged="Dp_DataInicio_SelectedDateChanged">
   <DatePicker.Resources>
      <Style TargetType="{x:Type DatePickerTextBox}">
         <Setter Property="Control.Template">
            <Setter.Value>
               <ControlTemplate>
                  <TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, StringFormat='dd/MM/yyyy HH:mm:ss'}" Background="#FF494949" Foreground="#FFEEEEEE" BorderThickness="0" IsReadOnly="False"/>
               </ControlTemplate>
            </Setter.Value>
         </Setter>
      </Style>
   </DatePicker.Resources>
</DatePicker>

And the C# I have is just this:

private void Dp_DataInicio_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
    if (Dp_DataInicio.SelectedDate > DateTime.Now) //Set the date to Now if is supperior to Now
    {
        Dp_DataInicio.SelectedDate = null;
        Dp_DataInicio.DisplayDate = DateTime.Now;
        Lbl_Erros.Text = "A data de inicio não pode ser após a este momento!";
    }
    else if (Dp_DataInicio.SelectedDate == Convert.ToDateTime(null)) //Letting the user know he cant have a null date
    {
        Dp_DataInicio.SelectedDate = null;
        Dp_DataInicio.DisplayDate = DateTime.Now;
        Lbl_Erros.Text = "A data de inicio tem de ter um valor.";
    }
    else if (Dp_DataInicio.SelectedDate >= Dp_DataFim.SelectedDate && Dp_DataFim.SelectedDate != Convert.ToDateTime(null)) //Not letting the beggining date be supperior to the finishing date
    {
        Dp_DataInicio.SelectedDate = null;
        Dp_DataInicio.DisplayDate = DateTime.Now;
        Lbl_Erros.Text = "A data de inicio não pode ser após a data de fim";
    }
    else //If it's all ok
    {
        Lbl_Erros.Text = null;
    }

    DateTime value;
    //Its Valid date
    if (DateTime.TryParse(Dp_DataInicio.Text, out value))
    {
        DataInicioValido = true;
    }
    else
    {
        DataInicioValido = false;
    }

    AtualizarBotoes(); //Change the state of other buttons
}

The date is always in dd/MM/yyyy hh:mm:ss format, but it changes the order of the month and the day when the element loses focus. but it is hadled like this. dd/MM/yyyy hh:mm:ss.

  • 2
    You are altering the format for the textbox used for editing but unfortunately the Control itself is still using the default thread culture and there is no way to provide a custom format. If the format you want follows a known culture, you could set the window to that culture when it is first created (see https://stackoverflow.com/questions/16565652/set-culture-for-a-wpf-datetimepicker-validation) – Jeff R. Jun 03 '19 at 20:37
  • I wonder if you can do it with a converter https://stackoverflow.com/a/24415748/1237135 – Jim W Jun 03 '19 at 22:22

1 Answers1

0

As Jeff R said, the solution is in this post (https://stackoverflow.com/a/16565917/9925729) And to get it working it's needed to add the using System.Windows.Markup; And the code

this.Language = XmlLanguage.GetLanguage("fr-FR");

After InitializeComponent();