1

So in my (large) project, I've got a special date box that derives from a masked text box and thus has a "Format" property. It works by giving it a short date format, such as:

<extended:DateMaskTextBox Format="yyyy-MM-dd" />

Now at the start of the application, the ShortDatePattern property of the CultureInfo.CurrentCulture.DateTimeFormat object is set, depending on some logic. I would like to obtain this ShortDatePattern to use it as a value for my Format property of my DateMaskTextBox.

So far I've got this, and it is not working:

<extended:DateMaskTextBox Format="{Binding Source={x:Static glob:CultureInfo.CurrentCulture}, Path=DateTimeFormat.ShortDatePattern}" />

Any idea why this does not work? Thanks.

WPFNewbie Wannabe
  • 198
  • 1
  • 2
  • 8

1 Answers1

0

That Binding syntax is correct and should work just fine. Are you getting any binding errors in your output window? Have you tried displaying that same Binding on a TextBlock.Text in place of your control to just see if the string is coming through?

Without seeing its code I can't tell but I suspect that the problem may be with the DateMaskTextBox rather than the Binding. If the control is not set up correctly to respect newly set values of its Format property the bound value may not be getting used at all or be getting used too late to affect the behavior you're seeing when interacting with the control.

John Bowen
  • 24,213
  • 4
  • 58
  • 56
  • Thank you for your response. I tried adding a TextBlock with its Text property using that binding, and it has indeed worked correctly. So the issue must be with the DateMaskTextBox. Note that the Format property is just a regular old property, is this okay to be using a Binding with? One thing I don't like right now is my breakpoint in the property setter doesn't always go off. If I hard code the Format value to something, my breakpoint gets hit in Debug mode. But when I set the Format property to this binding, my breakpoint does not get hit. Any reason for this? – WPFNewbie Wannabe Jan 28 '13 at 15:06
  • Bindings can only be set on a DependencyProperty (as the target) so if you're just using a standard get/set property it's not going to work at all. Look at http://msdn.microsoft.com/en-us/library/ms752914.aspx for some good explanations and examples of using DPs. Be aware that even after you set up a DP, you still won't hit breakpoints in the setter because Binding calls SetValue directly. – John Bowen Jan 28 '13 at 16:28
  • Yep, that was my problem. I added a DependencyProperty for my property, and I had to listen to the OnPropertyChanged event to properly set my property value. Thanks John! TL;DR version: My binding was correct, I just needed a DependencyProperty for my property so the Binding would work. – WPFNewbie Wannabe Jan 28 '13 at 18:40