locked
How to change the default time of date time picker from 12:00AM in wpf RRS feed

  • Question

  • <etk:DateTimePicker Name="dtFromSlot" Margin="0,0,5,5" Height="24" Value="{Binding LPSearch.LoadingTimeFrom}"> 
    Monday, July 21, 2014 12:23 PM

Answers

  • Hi,

    Currently, there is no DateTimePicker control in native Wpf, looks like that you are using a third-party control, for instance, this one: https://wpftoolkit.codeplex.com/

    If so, this control has a propery named DefaultValue, you can set defaule datetime as you like:

    <xctk:DateTimePicker HorizontalAlignment="Left"
          Margin="10,100,0,0" VerticalAlignment="Top" DefaultValue="06/29/2014 06:00:00"/>

    Screenshot:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 22, 2014 6:35 AM
  • I spent some time looking into this.

    Using the datetimepicker in this way is going to be pretty tricky.

    I would suggest you instead use two controls:

    A DatePicker and a TimePicker

    https://wpftoolkit.codeplex.com/wikipage?title=TimePicker

    Then put the two together.

    You can easily default the TimePicker time to 6am.

    I would suggest setting the starttime property to 6am.

    Bind the DatePicker to Dated, the TimePicker to JustTime and you get the two together in FullDateTime:

            private DateTime fullDateTime;
    
            public DateTime FullDateTime
            {
                get { return fullDateTime; }
                set 
                { 
                    fullDateTime = value; 
                }
            }
            
            private DateTime dated;
    
            public DateTime Dated
            {
                get { return dated; }
                set 
                { 
                    dated = value;
                    fullDateTime = Dated.Add(JustTime.TimeOfDay);
                }
            }
    
            private DateTime justTime;
    
            public DateTime JustTime
            {
                get { return justTime; }
                set 
                { 
                    justTime = value;
                    fullDateTime = Dated.Add(JustTime.TimeOfDay);
                }
            }

    Tuesday, July 22, 2014 8:29 AM

All replies

  • Have you tried setting

    LoadingTimeFrom = YourDefaultDateTime;

    When you new up whatever the datacontext is.

    Either that or set the TargetNullValue in the binding.

    You will need

    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    and something like
    <etk:DateTimePicker Name="dtFromSlot" Margin="0,0,5,5" Height="24" Value="{Binding LPSearch.LoadingTimeFrom, TargetNullValue='{x:Static sys:DateTime.Today}'}">

    Monday, July 21, 2014 1:11 PM
  • Hi Andy this is what I tried

    public DateTime LoadingTimeFrom
            {
                get { return _LoadingTimeFrom; }
                set
                { 
                    _LoadingTimeFrom = value;
                    _LoadingTimeFrom = _LoadingTimeFrom.AddHours(6);
                    RaisePropertyChanged("LoadingTimeFrom");
                }
            }

    LoadingTimeFrom = DateTime.Today.AddDays(-1);

    But the problem is everytime I click next time it jumps 6 hours. 

    


    Monday, July 21, 2014 1:29 PM
  • I actually want to change the default time from 12:00:00AM to 6:00:00AM
    Monday, July 21, 2014 1:31 PM
  • If you want to change the increment for time as you click then there's some code on codeplex:

    https://wpftoolkit.codeplex.com/discussions/355784

    Your code is the problem - as soon as you set it, it'll add 6 hours.

    You want something more like

    private DateTime _LoadingTimeFrom  = DateTime.Today.AddHours(6); 
    
    public DateTime LoadingTimeFrom
             {
                 get { return _LoadingTimeFrom; }
                 set
                 { 
                     _LoadingTimeFrom = value;
    
                     RaisePropertyChanged("LoadingTimeFrom");
                 }
             }

    Monday, July 21, 2014 1:45 PM
  • I tried this "private DateTime _LoadingTimeFrom = DateTime.Today.AddHours(6);" and its default to 6:00AM but if you click on date again it will go back to 12:00AM


    Monday, July 21, 2014 4:41 PM
  • So the default time is working.

    Did you follow the link?

    https://wpftoolkit.codeplex.com/discussions/355784

    Monday, July 21, 2014 5:45 PM
  • May be you can try something like this.

    public DateTime LoadingTimeFrom
            {
                get { return _LoadingTimeFrom; }
                set
                { 
                    _LoadingTimeFrom = value;
                     if (_LoadingTimeFrom = new DateTime())
                     {
                        _LoadingTimeFrom = _LoadingTimeFrom.AddHours(6);
                     }
                    
                    RaisePropertyChanged("LoadingTimeFrom");
                }
            }


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    That's a direct copy of the code I posted.

    Monday, July 21, 2014 6:28 PM
  • Try following code change to "Andy ONeill" code

    private DateTime _LoadingTimeFrom = DateTime.Today.AddHours(6);
    
            public DateTime LoadingTimeFrom
            {
                get { return _LoadingTimeFrom; }
                set
                {
                    _LoadingTimeFrom = value;
                    if (_LoadingTimeFrom.TimeOfDay == new TimeSpan())
                    {
                        _LoadingTimeFrom = _LoadingTimeFrom.AddHours(6);
                    }
                    RaisePropertyChanged("LoadingTimeFrom");
                }
            }


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor


    Monday, July 21, 2014 6:56 PM
  • That won't work.

    When the user decrements it'll go up 6 hours from the original.

    When they pick a date.

    It'll be ignored and it'll go up 6 hours from the original.


    Monday, July 21, 2014 7:33 PM
  • Hi,

    Currently, there is no DateTimePicker control in native Wpf, looks like that you are using a third-party control, for instance, this one: https://wpftoolkit.codeplex.com/

    If so, this control has a propery named DefaultValue, you can set defaule datetime as you like:

    <xctk:DateTimePicker HorizontalAlignment="Left"
          Margin="10,100,0,0" VerticalAlignment="Top" DefaultValue="06/29/2014 06:00:00"/>

    Screenshot:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 22, 2014 6:35 AM
  • I spent some time looking into this.

    Using the datetimepicker in this way is going to be pretty tricky.

    I would suggest you instead use two controls:

    A DatePicker and a TimePicker

    https://wpftoolkit.codeplex.com/wikipage?title=TimePicker

    Then put the two together.

    You can easily default the TimePicker time to 6am.

    I would suggest setting the starttime property to 6am.

    Bind the DatePicker to Dated, the TimePicker to JustTime and you get the two together in FullDateTime:

            private DateTime fullDateTime;
    
            public DateTime FullDateTime
            {
                get { return fullDateTime; }
                set 
                { 
                    fullDateTime = value; 
                }
            }
            
            private DateTime dated;
    
            public DateTime Dated
            {
                get { return dated; }
                set 
                { 
                    dated = value;
                    fullDateTime = Dated.Add(JustTime.TimeOfDay);
                }
            }
    
            private DateTime justTime;
    
            public DateTime JustTime
            {
                get { return justTime; }
                set 
                { 
                    justTime = value;
                    fullDateTime = Dated.Add(JustTime.TimeOfDay);
                }
            }

    Tuesday, July 22, 2014 8:29 AM