c# - 无法在 MVVM 中正确获取 UWP 行为中的 CalendarDatePicker

标签 c# mvvm win-universal-app

UWP 应用非常简单。具有由包含 CalendarDatePicker 的页面绑定(bind)的属性的 ViewModel。

查看模型:

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 属性绑定(bind)到虚拟机的日期并运行应用程序时,日期默认为 1916 年 4 月 16 日。

我尝试使 Date 可以为 null,将绑定(bind)模式更改为 TwoWay 以及许多其他操作都无济于事。

有没有人遇到过同样的问题?如何在 MVVM 中使用此控件?

最佳答案

The moment I bind the CalendarDatePicker Date property to my vm's Date and run the app, the date defaults to April 16, 1916.

我不知道到底是什么导致了这个问题,但它返回了一个 DateMinDate 小.

When you set the Date in code, the value is constrained by the MinDate and MaxDate properties. If Date is smaller than MinDate, the value is set to MinDate. If Date is greater than MaxDate, the value is set to MaxDate.

可以引用CalendarDatePicker class看到这个,由你MinDate是“1916 年 4 月 16 日”,我现在是“3/15/1916 4:27:52 PM +08:00”。

但是我发现你的课有问题MainPageViewModel : INotifyPropertyChanged .

如果您检查 CalendarDatePicker.Date property , 你会发现 Date不是 DateTimeOffset 的类型, 它的类型是 Nullable<DateTimeOffset> .

这就是我让它工作的方式。 主页 XAML 代码:

<Page
    x:Class="CalendarDatePickerInMVVM.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CalendarDatePickerInMVVM"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <CalendarDatePicker x:Name="picker" HorizontalAlignment="Center"  Date="{x:Bind MainPageViewModel.Date}" />
        <TextBox x:Name="txt" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" />
    </Grid>
</Page>

MainPage.cs 背后的代码:

public sealed partial class MainPage : Page
{
    private ViewModel.MainPageViewModel MainPageViewModel;
    public MainPage()
    {
        this.InitializeComponent();
        txt.Text = picker.MinDate + "......" + picker.MaxDate;
        MainPageViewModel = new ViewModel.MainPageViewModel();
    }
}

和类MainPageViewModel : INotifyPropertyChanged :

public class MainPageViewModel : INotifyPropertyChanged
{
    private System.Nullable<DateTimeOffset> date;

    public MainPageViewModel()
    {
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public System.Nullable<DateTimeOffset> Date
    {
        get { return date; }
        set
        {
            date = value;
            OnPropertyChanged();
        }
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

如果你想给你的 CalendarDatePicker 一个默认值,你可以这样编码:

private System.Nullable<DateTimeOffset> date = new DateTime(2016, 3, 15);

关于c# - 无法在 MVVM 中正确获取 UWP 行为中的 CalendarDatePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35977726/

相关文章:

c# - 在 Xamarin C# 中向 MKMapView 的边缘添加填充

c# - 在代码后面创建带有Caliburn事件的对象

c# - 在 UWP 中使用 PCL resx 字符串

c# - 用于许多并发客户端和数据库访问的 WCF 服务

c# - 如何将 JSON 对象转换为自定义 C# 对象?

mvvm - UWP 属性在用户控件的依赖属性中更改

c# - 从 C# 加载 C++ Windows 运行时组件时出现异常

xamarin - 我们可以使用 Xamarin.Forms for UWP 进行流畅的设计吗?

c# - 如何拉伸(stretch)位图以填充 PictureBox

c# - WPF Datagrid主详细信息MVVM