Hikari
Hikari

Reputation: 599

UWP DataGrid Datetime binding issue

I have a sample project with an DataGrid that have a DataGridTemplateColumn that have as DataTemplate a CalendarDatePicker,

If I try to add a row to the binded ItemSource, the date is always 01/01/1920.

I have check inside the ItemSource, and the value is correct (es. DateTime.Now).

I have attached the project.

How can I solve this issue?

Project

enter image description here

Upvotes: 0

Views: 51

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32775

UWP DataGrid Datetime binding issue

That's interesting, I checked your code, I found DataLavoro type is DateTime, but Date DependencyProperty allowed type is DateTimeOffset. I edit your code like the following and CalendarDatePicker works.

Xaml

private void AggiungiRiga()
{
    DateTimeOffset adesso = DateTimeOffset.Now;
    ListaOrari.Add(new Orario
    {
        Id = 5,
        DataLavoro = adesso,
        Inizio = new TimeSpan(adesso.Hour, adesso.Minute, 0)
    });
}

Model

public DateTimeOffset DataLavoro
{
    get { return _dataLavoro; }

    set { _dataLavoro = value; }
}
private DateTimeOffset _dataLavoro;

Upvotes: 1

Related Questions