输入后 DatePicker 从 dd/MM/yyyy 变为 MM/dd/yyyy
DatePicker is changing from dd/MM/yyyy to MM/dd/yyyy after input
我正在做一个 wpf 应用程序,我有两个字段必须有这样的日期:dd/MM/yyyy hh:mm:ss
但是当我从元素上失去焦点时,日期会变为 MM/dd/yyyy hh:mm:ss
。这只会在我更改日期时发生。
如果我改变一秒月和日开关。
如果我输入“03/06/2019 16:58:00”,当它失去焦点时,日期是“06/03/2019 16:58:00”。这在 POPUP 中发生了变化。
问题不在于格式,而在于它显示的顺序。
我的XAML是这样的:
<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>
而我的 C# 就是这样的:
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
}
日期始终采用dd/MM/yyyy hh:mm:ss
格式,但它会在元素失去焦点时更改月份和日期的顺序。但它是这样处理的。 dd/MM/yyyy hh:mm:ss
.
如 said, the solution is in this post ()
为了让它工作,需要添加 using System.Windows.Markup;
和代码
this.Language = XmlLanguage.GetLanguage("fr-FR");
InitializeComponent();
之后
我正在做一个 wpf 应用程序,我有两个字段必须有这样的日期:dd/MM/yyyy hh:mm:ss
但是当我从元素上失去焦点时,日期会变为 MM/dd/yyyy hh:mm:ss
。这只会在我更改日期时发生。
如果我改变一秒月和日开关。
如果我输入“03/06/2019 16:58:00”,当它失去焦点时,日期是“06/03/2019 16:58:00”。这在 POPUP 中发生了变化。
问题不在于格式,而在于它显示的顺序。
我的XAML是这样的:
<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>
而我的 C# 就是这样的:
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
}
日期始终采用dd/MM/yyyy hh:mm:ss
格式,但它会在元素失去焦点时更改月份和日期的顺序。但它是这样处理的。 dd/MM/yyyy hh:mm:ss
.
如using System.Windows.Markup;
和代码
this.Language = XmlLanguage.GetLanguage("fr-FR");
InitializeComponent();