locked
Show DisplayDateStart as 2 years less than current date in WPF Datepicker RRS feed

  • Question

  • Hi Experts,

    I wants to display Datepicker startupdate as  2 years less than current date in WPF Datepicker.

    Currently I am using below code, after using this code, user is not able to select past date, I want users to select past date as well.

    Please help.

    DisplayDateStart="{Binding Source={x:Static sys:DateTime.Today}

    Thanks,
    Rahul

    Tuesday, July 28, 2015 3:04 PM

Answers

  • Set the DisplayDate property of the DatePicker to a date two years back in time:

    dp.DisplayDate = DateTime.Now.AddYears(-2);
    <DatePicker x:Name="dp"/>


    Don't set the DisplayDateStart property if you want the user to be able to select any date.

    You cannot bind to a static built-in property that returns a date from two years ago but you could bind to your own custom DateTime property if you want to.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by rahul_agg11 Wednesday, July 29, 2015 8:54 AM
    Tuesday, July 28, 2015 3:12 PM
  • How are you going to get the date the user chooses?

    Personally I would use mvvm and just set a default date on a bound property.

    So in my window

        <Window.DataContext>
            <local:MainWindowViewModel/>
        </Window.DataContext>
        <StackPanel>
            <DatePicker SelectedDate="{Binding ChosenDate}"/>

    and that viewmodel

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.CommandWpf;
    using GalaSoft.MvvmLight.Messaging;
    using System.Collections.ObjectModel;
    using System.Diagnostics;
    namespace wpf10
    {
        public class MainWindowViewModel : ViewModelBase
        {
            private DateTime chosenDate = DateTime.Now.AddYears( -2);
    
            public DateTime  ChosenDate
            {
                get { return chosenDate; }
                set 
                {
                    chosenDate = value;
                    RaisePropertyChanged();
                }
            }

    I'm using mvvmlight there.

    You could just implement inotifypropertychanged yourself:

    https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    And substitute NotifyPropertyChanged there instead of RaisePropertychanged if you prefer.


    • Marked as answer by rahul_agg11 Wednesday, July 29, 2015 8:54 AM
    Tuesday, July 28, 2015 4:56 PM

All replies

  • Set the DisplayDate property of the DatePicker to a date two years back in time:

    dp.DisplayDate = DateTime.Now.AddYears(-2);
    <DatePicker x:Name="dp"/>


    Don't set the DisplayDateStart property if you want the user to be able to select any date.

    You cannot bind to a static built-in property that returns a date from two years ago but you could bind to your own custom DateTime property if you want to.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by rahul_agg11 Wednesday, July 29, 2015 8:54 AM
    Tuesday, July 28, 2015 3:12 PM
  • How are you going to get the date the user chooses?

    Personally I would use mvvm and just set a default date on a bound property.

    So in my window

        <Window.DataContext>
            <local:MainWindowViewModel/>
        </Window.DataContext>
        <StackPanel>
            <DatePicker SelectedDate="{Binding ChosenDate}"/>

    and that viewmodel

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.CommandWpf;
    using GalaSoft.MvvmLight.Messaging;
    using System.Collections.ObjectModel;
    using System.Diagnostics;
    namespace wpf10
    {
        public class MainWindowViewModel : ViewModelBase
        {
            private DateTime chosenDate = DateTime.Now.AddYears( -2);
    
            public DateTime  ChosenDate
            {
                get { return chosenDate; }
                set 
                {
                    chosenDate = value;
                    RaisePropertyChanged();
                }
            }

    I'm using mvvmlight there.

    You could just implement inotifypropertychanged yourself:

    https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    And substitute NotifyPropertyChanged there instead of RaisePropertychanged if you prefer.


    • Marked as answer by rahul_agg11 Wednesday, July 29, 2015 8:54 AM
    Tuesday, July 28, 2015 4:56 PM