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.

4
  • See stackoverflow.com/questions/3819832/… Commented Aug 8, 2019 at 8:47
  • Changing string format of DatePicker's TextBox by style only affects itself, not binding. Commented Aug 8, 2019 at 9: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´´´ Commented Aug 8, 2019 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. Commented Aug 9, 2019 at 7:47

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.