【发布时间】:2016-06-28 21:53:30
【问题描述】:
UWP 应用非常简单。具有属性的 ViewModel 由包含 CalendarDatePicker 的页面绑定。
查看模型:
public class MainPageViewModel : INotifyPropertyChanged
{
private DateTimeOffset date;
public MainPageViewModel()
{
}
public event PropertyChangedEventHandler PropertyChanged;
public DateTimeOffset Date
{
get { return date; }
set
{
date = value;
OnPropertyChanged();
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
页面:
<Page
x:Class="App2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.DataContext>
<local:MainPageViewModel />
</Page.DataContext>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<CalendarDatePicker />
</Grid>
</Page>
我运行这个应用程序,CalendarDatePicker 的行为符合预期。启动时未选择日期,显示“选择日期”字眼,展开时默认为今天的日期。
当我将 CalendarDatePicker Date 属性绑定到我的虚拟机的日期并运行应用程序时,日期默认为 1916 年 4 月 16 日。
我尝试将Date 设为可空,将绑定模式更改为TwoWay 以及许多其他方法均无济于事。
有没有人遇到过同样的问题?如何在 MVVM 中使用此控件?
【问题讨论】:
标签: c# mvvm win-universal-app