Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将日期绑定到日期选择器在UWP中不起作用_C#_Xaml_Datepicker_Uwp - Fatal编程技术网

C# 将日期绑定到日期选择器在UWP中不起作用

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

我有一个DateTime数据类型变量,它绑定到一个日期选择器

日期选择器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日。我已使用您需要的实现更新了我的答案:-)