Can't seem to get Timepicker to work (C# / WPF)
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 KeyDow开发者_Python百科n 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.
There is an official Microsoft DateTimePicker control included in the WPF Toolkit. This will be part of the framework in .NET 4.0
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.
There's a DatePicker and a TimePicker in silverlight 4
If you want you can create your own control with those or simply use each one separated.
精彩评论