简体   繁体   中英

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. I get error like this:

Unable to cast object of type 'Xamarin.Forms.Xaml.ElementNode' to type 'Xamarin.Forms.Xaml.ValueNode'

I store dates in SQLite as strings. Here is the Model class part:

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

This is the xaml part of the view:

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

This is the 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:

<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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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