如何在datepicker中显示当前日期

时间:2014-08-22 14:45:13

标签: c# asp.net silverlight silverlight-4.0

使用c#和silverlight

我想在datepicker中显示当前日期

控制代码

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

在c#中尝试

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

如何显示日期,需要代码帮助

2 个答案:

答案 0 :(得分:2)

尝试设置SelectedDate值。

cAccCreditDate.SelectedDate = DateTime.Now;

答案 1 :(得分:0)

我的建议是将绑定添加到DatePicker,如下所示:

SelectedDate="{Binding DateToBind, Mode=TwoWay}

然后设置DateToBind的值。

<强> UPD:

示例代码(注意:它只是几分钟内创建的示例)

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;
        }
    }
}