Gopal
Gopal

Reputation: 11982

How to display current date in datepicker

Using c# and silverlight

I want to display a current date in datepicker

Control Code

<sdk:DatePicker Name="cAccCreditDate"
                Margin="10,0,0,0"
                DisplayDate="12/12/2014"
                HorizontalAlignment="Left" VerticalAlignment="Center"  
 Width="120" Height="23" />

Tried in c#

cAccCreditDate.DisplayDate = DateTime.Now.Date; --date is not displaying
cAccCreditDate.DisplayDate = DateTime.Now; -- date is not displaying

How to display a date, need code help

Upvotes: 0

Views: 2294

Answers (2)

Greenonion
Greenonion

Reputation: 87

My suggestion is to add binding to DatePicker like this:

SelectedDate="{Binding DateToBind, Mode=TwoWay}

and then set value of DateToBind.

UPD:

Sample code (note: it's just a sample created in couple of minutes)

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            DataContext = new ViewModel();
        }
    }

    public class ViewModel: INotifyPropertyChanged
    {
        private DateTime _currentDateTime;
        public DateTime CurrentDateTime
        {
            get { return _currentDateTime; } 
            set { 
                _currentDateTime = value;
                OnPropertyChanged("CurrentDateTime");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        public ViewModel()
        {
            CurrentDateTime = DateTime.Now;
        }
    }
}

Upvotes: 0

Pascal_AC
Pascal_AC

Reputation: 541

Try to set the SelectedDate value.

cAccCreditDate.SelectedDate = DateTime.Now;

Upvotes: 2

Related Questions