Search code examples
c#wpfxamlmahapps.metro

MahApps DateTimePicker custom datetime format


I am using the DateTimePicker control of MahApps.

<controls:DateTimePicker 
    SelectedDate="{Binding SelectedDateTime, RelativeSource={RelativeSource AncestorType=UserControl}}" 
    SelectedTimeFormat="Short" 
    SelectedDateFormat="Short"
    Culture="en-In"/>

Currently it's showing datetime in dd-mm-yyyy HH:mm format.But I need to show that in yyyy-mm-dd HH:mm Z format. I see currently it has no property which takes datetime format as string pattern. So how to do this?


Solution

  • Yeah, the control doesn't support this. You can override the GetValueForTextBox method to achieve this

    public class MyDateTimePicker : DateTimePicker
    {
        protected override string GetValueForTextBox()
        {
            return SelectedDate?.ToString("yyyy-MM-dd HH:mm Z");
        }
    }
    

    XAML

    <local:MyDateTimePicker />