9

I can't get my custom DateTime string format to work in my binding. I want the format to be "mmmm, yyyy" (e.g. "June, 2012").

The following does not work. I get a short date format (m/d/yyyy).

<TextBlock Text="{Binding ElementName=ThisWindow,
                          Path=Date,
                          StringFormat={}{0:MMMM\, yyyy}"/>

I've considered using a converter, but I prefer a pure XAML approach.

Edit:

For clarity, I have a Window with a dependency property Date of type DateTime. In my XAML, I've named the window 'Thiswindow'.

Edit 2:

I looked back at my actual code, and I had a Label, not a TextBlock. I changed it to TextBlock and it works fine.

<Label Content="{Binding ElementName=ThisWindow,
                 Path=Date,
                 StringFormat={}{0:MMMM\, yyyy}"/>

Anyone know why it doesn't work with Label?

Thanks.

2
  • This worked for me (except it should be {0:MMMM\, yyyy}). Something about your "Date" property? Is it by chance returning a string instead of a DateTime? Commented Jun 8, 2012 at 16:55
  • I'll edit to make the situation clearer... Commented Jun 8, 2012 at 16:57

2 Answers 2

12

ContentControls have a ContentStringFormat property which overrides the original formatting.

(When i saw your question i expected this to be the problem actually but was surprised to find a TextBlock at first)

Sign up to request clarification or add additional context in comments.

Comments

2

Your month needs to be in uppercase:

{Binding Source={x:Static sys:DateTime.Now}, StringFormat={}{0:MMMM\, yyyy}}

EDIT:

The Label problem is probably because Label has Content, not Text.

Change the Text="{Binding ...}" to Content="{Binding ...}"

1 Comment

I had it as a Content property when it was a Label, and it wasn't working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.