简体   繁体   English

将DatePicker绑定到C#Xamarin.Forms中的Model类的字符串属性

[英]Binding DatePicker to string property of class Model in C# Xamarin.Forms

I am trying to set DatePicker default date to some current date and bind DateSelected to string property of my object. 我试图将DatePicker默认日期设置为某个当前日期,并将DateSelected绑定到我的对象的字符串属性。 I get error like this: 我收到这样的错误:

Unable to cast object of type 'Xamarin.Forms.Xaml.ElementNode' to type 'Xamarin.Forms.Xaml.ValueNode' 无法将类型为“ Xamarin.Forms.Xaml.ElementNode”的对象转换为类型“ Xamarin.Forms.Xaml.ValueNode”

I store dates in SQLite as strings. 我将日期存储在SQLite中作为字符串。 Here is the Model class part: 这是Model类的一部分:

    public string Arrivo { get; set; }
    public string Partenza { get; set; }

This is the xaml part of the view: 这是视图的xaml部分:

    <DatePicker x:Name="pickArrivalDate" DateSelected="{Binding Arrivo}"/>
    <DatePicker x:Name="pickDepartureDate" DateSelected="{Binding Partenza}"/>

This is the ViewModel: 这是ViewModel:

    public string Arrivo
    {
        get => _tavolo.Arrivo;
        set
        {
            _tavolo.Arrivo = value;
            NotifyPropertyChanged("Arrivo");
        }
    }

    public string Partenza
    {
        get => _tavolo.Partenza;
        set
        {
            _tavolo.Partenza = value;
            NotifyPropertyChanged("Partenza");
        }
    }

I guess I should do the conversion but not sure how to achieve this. 我想我应该进行转换,但不确定如何实现。 Any ideas? 有任何想法吗?

You have to set Date property because DateSelected is an event: 您必须设置Date属性,因为DateSelected是一个事件:

<DatePicker x:Name="pickArrivalDate" Date="{Binding Arrivo}"/>
<DatePicker x:Name="pickDepartureDate" Date="{Binding Partenza}"/>

Note: Properties Arrivo and Partenza, have to be DateTime, so if they are string, you have to use a ValueConverter . 注意:属性Arrivo和Partenza必须为DateTime,因此,如果它们是字符串,则必须使用ValueConverter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM