wpfdatepickerpersian-calendar

WPF : the best way of PersianDatePicker Display dateFormat short


enter image description here as you look into picture, I want to display short form of date by means of "yyyy/mm/dd" into my TextBox OR PersianDatePicker. but when I used this Code for PersianDatePicker in XAML:

    <PersianDateControls:PersianDatePicker x:Name="ImgDateReq"/>

And in C#:

    ImgDateReq.Text = query.ImgDateRequest.ToString();

I Get this Error: Error 3 Cannot implicitly convert type 'string' to 'Arash.PersianDate'

and when I use a DatePicker in XAML:

    <DatePicker x:Name="D1" />

in C#

    D1.Text = query.ImgDateRequest.ToString();

I get the result. how to display my PersianDateControl in short format like the DatePicker


Solution

  • For the first issue you need to convert the string into Arash.PersianDate.

    The following example is an illustration of what it might look like and NOT the exact solution:

    string dtString = query.ImgDateRequest.ToString()
    ImgDateReq.Text = new PersianDate(dtString);
    

    You will need to find out how you turn that string into a Arash.PersianDate object.


    On a separate note, it seems like you're not using the MVVM pattern which is usually an incorrect way to approach WPF. Currently you are updating the UI element properties yourself which is only done in special cases and/or when neccessary.

    What I would recommend is looking into the MVVM pattern for WPF.

    From there, once you have a ViewModel and bound said ViewModel to the View, you can use data binding and converters to bind and convert your DateTime string.

    Furthermore, you will also need to use the PropertyChanged notifications to update the UI.