0

I want to bind DatePicker to string property of viewmodel. Trouble is to set proper string format. It's problem-specific to store date in string property.

Binding works fine except string format. My xaml is:

<DatePicker SelectedDate="{Binding Value, StringFormat=dd.MM.yyyy}"/>

I expect of string 15.08.2019, but string is 8/15/2019 12:00:00 AM

[Edit] This question is not duplicate. Changing string format of DatePicker's TextBox by style only affects itself, not binding.

  • See https://stackoverflow.com/questions/3819832/changing-the-string-format-of-the-wpf-datepicker – auburg Aug 08 '19 at 08:47
  • Changing string format of DatePicker's TextBox by style only affects itself, not binding. – Michael Mankiewicz Aug 08 '19 at 09:43
  • It's probably a better idea to change the ViewModel so that it Outputs a ´´´DateTime´´´ property. You could parse the string you already have through ´´´DateTime.Parse´´´ – S Schulze Aug 08 '19 at 10:39
  • It's not possible. One row is `ExpandoObject` with custom oblects, depending on the type of "column" (`Enum`) `DataTemplateSelector` returns appropriate editor for cell... All data stored in `string`. – Michael Mankiewicz Aug 09 '19 at 07:47

1 Answers1

0

You need to Change the Template of DatePickerTextBox

<DatePicker SelectedDate="{Binding Path = Value, StringFormat = {}{0:dd.MM.yyyy}}">
    <DatePicker.Resources>
        <Style TargetType="DatePickerTextBox">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, StringFormat = {}{0:dd.MM.yyyy}, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DatePicker.Resources>
</DatePicker>
Aakanksha
  • 329
  • 2
  • 7
  • This xaml changes string format in DatePiker's editor, but not applies to viewmodel's property. I already tried this solution and it doesn't work for me. – Michael Mankiewicz Aug 08 '19 at 09:38