wpftoolkit icon indicating copy to clipboard operation
wpftoolkit copied to clipboard

DateTimePicker - when Value is set the only date portion is shown on control but time part remains 12:00 AM

Open ilativity opened this issue 5 years ago • 10 comments

ilativity avatar Feb 13 '20 09:02 ilativity

Hi, If you define a DatetimePicker like this: <xctk:DateTimePicker Value="2020/01/02"/> then yes, the default time part is 12:00 AM.

But if you define a DateTimePicker like this: <xctk:DateTimePicker /> and selects a date from the calendar, or by using the up/down buttons, the time part will use the current time.

XceedBoucherS avatar Feb 18 '20 19:02 XceedBoucherS

Hi,

I put the value from the code behind. Something like this:

Xaml <xctk:DateTimePicker x:Name="dateTimePicker"/>

cs file dateTimePicker.Value = new DateTime(2020, 02 , 19, 23, 50, 11);

ilativity avatar Feb 19 '20 21:02 ilativity

Using v3.8, I can see a DateTimePicker showing : "Wednesday, February 19, 2020 11:50:11 PM"

XceedBoucherS avatar Feb 21 '20 20:02 XceedBoucherS

@XceedBoucherS I was able to reproduce this problem in v3.8.1.

If you put this in de cs file:

dateTimePicker.Value = new DateTime(2020, 02, 19, 23, 50, 11);
dateTimePicker.Value = new DateTime(2020, 02, 20, 0, 0, 0);

You should get 'Thursday, February 20, 2020 00:00:00". But when executing this code you will get 'Thursday, February 20, 2020 11:50:11 PM".

TimKras avatar Jun 11 '20 14:06 TimKras

Hi TimKras, I installed Toolkit v3.8.1 via Nuget and this is my configuration: <StackPanel> <xctk:DateTimePicker x:Name="dateTimePicker"/> </StackPanel>

public MainWindow() { InitializeComponent();

  dateTimePicker.Value = new DateTime( 2020, 02, 19, 23, 50, 11 );
  dateTimePicker.Value = new DateTime( 2020, 02, 20, 0, 0, 0 );
}

Result is as expected : 'Thursday, February 20, 2020 00:00:00" I even tried using a button and on click setting the 2nd line: dateTimePicker.Value = new DateTime( 2020, 02, 20, 0, 0, 0 ); and it still worked.

XceedBoucherS avatar Jun 12 '20 13:06 XceedBoucherS

Can you try this code. (both lines in the Click event).

public MainWindow()
{
   InitializeComponent();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
   dtp.Value = new DateTime(2020, 02, 19, 23, 50, 11);
   dtp.Value = new DateTime(2020, 02, 20, 0, 0, 0);
}

TimKras avatar Jun 12 '20 13:06 TimKras

Thank you for the tip, I was able to reproduce the bug. This only happens in code-behind, when modifying more than once the DateTimePicker.Value. It will be fixed in v4.1. In the meantime, I suggest you calculate the wanted dateTime and sets it once instead of calling dtp.Value many times.

Thank you.

XceedBoucherS avatar Jun 15 '20 16:06 XceedBoucherS

Hi.

I'm having the same issue, but in my case value is binded to ViewModel value. Sometimes when property in viewModel is updated only the date portion in DateTimePicker changes, but the time portion remains unchanged.

11v1 avatar Feb 04 '21 10:02 11v1

So maybe you need to wait for v4.1 to have the fix.

If not, you can go in file Xceed.Wpf.Toolkit/DateTimePicker/Implementation/DateTimePicker.cs and:

  1. add a new bool variable
  2. in OnValueChanged() method, set the new variable to true before _calendar.SelectedDate and set if back to false after setting _calendar.DisplayDate.
  3. in Calendar_SelectedDatesChanged() method, modify "this.Value" only when the new variable is false.

XceedBoucherS avatar Feb 08 '21 16:02 XceedBoucherS

It was under 4.0.1. Moving to 4.0.2 solved my problem.

11v1 avatar Feb 09 '21 02:02 11v1