使用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
如何显示日期,需要代码帮助
答案 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;
}
}
}