C# 将日期绑定到日期选择器在UWP中不起作用
我有一个DateTime数据类型变量,它绑定到一个日期选择器C# 将日期绑定到日期选择器在UWP中不起作用,c#,xaml,datepicker,uwp,C#,Xaml,Datepicker,Uwp,我有一个DateTime数据类型变量,它绑定到一个日期选择器 日期选择器XAML: <CalendarDatePicker Name="dpDate" VerticalAlignment="Center" Date="{Binding dpDateTime, ElementName=this, Mode=TwoWay}" DateChanged="dp_DateChanged"> </CalendarDatePicker> 但是da
日期选择器XAML:
<CalendarDatePicker Name="dpDate" VerticalAlignment="Center"
Date="{Binding dpDateTime, ElementName=this, Mode=TwoWay}"
DateChanged="dp_DateChanged">
</CalendarDatePicker>
但是datepicker没有选择当前日期,而是选择日期为1917年1月1日问题在于,您设置的日期类型为
DateTime
,而日历需要DateTimeOffset
通过将dpDateTime
属性的类型更改为DateTimeOffset
,可以非常轻松地解决此问题
总的来说,最好在任何地方使用DateTimeOffset
而不是DateTime
,因为它包含时区信息,当用户的时区发生变化时,您的代码会更加可靠
更新-INotifyPropertyChanged
您还必须实现INotifyPropertyChanged
,以便将值更改通知给数据绑定。首先,您必须在类中添加并实现INotifyPropertyChanged
接口:
public class Page : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
...
}
然后更新属性setter以使用setter:
private DateTimeOffset _dpDateTime = DateTime.Now;
public DateTimeOffset dpDateTime
{
get { return _dpDateTime; }
set
{
_dpDateTime = value;
NotifyPropertyChanged();
}
}
数据绑定功能要求目标属性引发
PropertyChanged
事件,因为后台数据绑定会观察此事件。问题在于,您设置的日期类型为DateTime
,而日历需要DateTimeOffset
通过将dpDateTime
属性的类型更改为DateTimeOffset
,可以非常轻松地解决此问题
总的来说,最好在任何地方使用DateTimeOffset
而不是DateTime
,因为它包含时区信息,当用户的时区发生变化时,您的代码会更加可靠
更新-INotifyPropertyChanged
您还必须实现INotifyPropertyChanged
,以便将值更改通知给数据绑定。首先,您必须在类中添加并实现INotifyPropertyChanged
接口:
public class Page : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
...
}
然后更新属性setter以使用setter:
private DateTimeOffset _dpDateTime = DateTime.Now;
public DateTimeOffset dpDateTime
{
get { return _dpDateTime; }
set
{
_dpDateTime = value;
NotifyPropertyChanged();
}
}
数据绑定功能要求目标属性引发
PropertyChanged
事件,因为幕后的数据绑定观察到此事件。我已将其更改为DateTimeOffset
,但它不起作用。请将dpDateTime
代码的更新代码添加到问题中,因此,我可以看到您在我的模型中使用了什么,我已经给出了dpDateTime
作为public DateTime offset dpDateTime{get;set;}
然后我给出了dpDateTime=DateTime.Now.ToUTC()代码>模型正在使用正确的值更新,但日历仅使用当前时间和日期,即1918年1月1日。我已使用您需要的实现更新了我的答案:-)我已将其更改为DateTimeOffset
,但它不起作用。请将dpDateTime
代码的更新代码添加到问题中,因此,我可以看到您在我的模型中使用了什么,我已经给出了dpDateTime
作为public DateTime offset dpDateTime{get;set;}
然后我给出了dpDateTime=DateTime.Now.ToUTC()代码>模型正在使用正确的值更新,但日历仅使用当前时间和日期,即1918年1月1日。我已使用您需要的实现更新了我的答案:-)