edongashi / WpfMaterialForms

Dynamically generated forms and dialogs in WPF
MIT License
51 stars 14 forks source link

StringFormat and DatePicker support #11

Closed redbaty closed 6 years ago

redbaty commented 6 years ago

Is it possible?

edongashi commented 6 years ago

Hi, I just checked. For old DateSchema there doesn't appear to be any StringFormat prop. I guess it can be added on view creation.

In v2 StringFormat is supported. But calendars show as textboxes (control not implemented here).

[Binding(StringFormat = "dd MMM yyyy")]
public DateTime MyDate { get; set; }
redbaty commented 6 years ago

Oh great, hadn't realized it, Thanks !

redbaty commented 6 years ago

Wait I've done some testing and I couldn't get it to work, am I doing something wrong ? Sorry for the inconvenience

        [DataGridColumn("Data de nascimento", "d")]
        [Field(Name = "Data de nascimento", Icon = PackIconKind.CalendarCheck)]
        [Value(Must.NotBeEmpty)]
        [Binding(StringFormat = "dd/MMM/yyyy")]
        public DateTime? BirthDate { get; set; }

image

edongashi commented 6 years ago

MMM is month as in 19/Dec/2017. Are you looking for MM as in 19/12/2017?

I fear this is not enough though, because StringFormat handles only how data is displayed but not parsed. I will look how to configure parsing and let you know in a few minutes.

edongashi commented 6 years ago

@redbaty I think I found an easy way. If you like a control to be converted according to a culture you can pass ConverterCulture, for example Portuguese-Brazil you would use:

[Binding(StringFormat = "dd/MM/yyyy", ConverterCulture = "pt-BR")]
public DateTime? BirthDate { get; set; }

but this won't be very customizable, so I created an abstraction over CultureInfo to allow adding custom ones:

// Registering a custom date format based on current culture
CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
ci.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
// ci.DateTimeFormat.LongDatePattern = "dd MMMM ......";
ConversionCulture.Set("myDateCulture", ci);

Now you can use myDateCulture in a field. Note the code above must be executed before any form is created.

[Binding(StringFormat = "dd/MM/yyyy", ConverterCulture = "myDateCulture")]
public DateTime? BirthDate { get; set; }
redbaty commented 6 years ago

@EdonGashi Thanks, it worked like a charm!