I'm sure I'm missing something very easy here. Pretty much any internet search for a Timepicker for C#/WPF points to this webpage: http://jobijoy.blogspot.com/2007/10/time-picker-user-control.html and as such I figured it would work correctly. However, when I copy/paste this into a user control and run it, the control shows up as it should, but when I click on the digits and press Up or Down, nothing happens. Even in debug mode with a break point on the switch case for the KeyDown event, nothing happens. It's not registering the KeyDown event. It's supposed to focus on the grid that contains the TextBlock, so I tried changing the KeyDown to the TextBlock, but to no avail. I cannot seem to get this to work! :( I'm using Visual Studio 2008.
A:
I think you're the control is not getting focused, for some reason. I tried it, too, and the event wouldn't fire for me, either. This post might offer some insight. Manually setting focus to the user control in the Loaded event didn't work for me, though.
The only thing that did work for me was doing all of the following:
- changing the TextBlock to a TextBox and moving the event there (focus is pretty evident with a TextBox)
- changing the cast in the event to FrameworkElement (which is where the Name property comes from) instead of Grid
- changing the case to the TextBox's name (instead of the grid's name)
- changing the event to a PreviewKeyDown (to get the cursor keys to register)
Of course, this only got the event to fire and register properly, the values don't seem to show up (even before I changed the code), but it handles the specific issue of the event not firing.
Mike Pateras
2010-01-28 16:22:07
Thank you very much for this help! I just changed the the TextBlocks to TextBoxs and KeyDown to PreviewKeyDown, and now it works! There is an infinite recursion error with the DependencyProperties, though.
Brandon
2010-01-29 13:47:41
A:
There is an official Microsoft DateTimePicker control included in the WPF Toolkit. This will be part of the framework in .NET 4.0
Phil Devaney
2010-01-29 14:29:53
You're right! I knew it was there but had never actually used it, I am quite surprised it only does dates. I use the DateTimeEditor from Actipro WPF Studio, but that isn't free.
Phil Devaney
2010-01-29 15:23:24
Yeah, this blog says it's supposed to be really simple, and I'm sure it would be if I understood wrappers and dependencies and data-binding, but I'm a newbie to C# and WPF. I've seen other questions like "Where can I find a TimePicker" on Stack Overflow and other similar websites, and most answers are "you can easily roll your own" and point to this blog. But this blog's code doesn't work, and if you don't understand the concepts, you're in a bind. lol
Brandon
2010-01-29 15:36:54