why does the wpftoolkit datepicker eat the return key down event?
I am wondering if anybody knows why the datepicker will pass standard keys to any parent control's key down routed event, but not the return key?
here's the xaml i wrote:
<WrapPanel Name="_wpParameters"
Grid.Row="0" Grid.开发者_开发问答Column="0"
Orientation="Horizontal"
Grid.IsSharedSizeScope="True"
Keyboard.KeyDown="_wpParameters_KeyDown" >
<!-- this is where the dynamic parameter controls will be added -->
</WrapPanel>
Here is the code i was using to check for the return key:
private void _wpParameters_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
RaiseEvent(new RoutedEventArgs(LoadLiveResultsEvent, this));
}
}
I was using key down on accident (meant to use key up) but I found it interesting that the standard numeric and / characters were firing the logic, but not the return key. Any Idea's why the return key is not included as a key down key?
The KeyDown event is a lower-level text input event that might not behave as expected on certain controls. This is because some controls have control compositing or class handling that provides a higher-level version of text input handling and related events.
As viewed on MSDN...my assumption is that the control is consuming the event and perhaps committing the text to the bindable source and other cleanup and then marking the event as handled.
In addition had to mention my solution. I had a parent view, that handle the keyDown event from all child viewmodels. I declared a behavior for special controls like DatePicker,MaskedTextBox,etc... that catch previewKeyDown tunneling event and raise KeyDown bubbling event:
public class EnterPressedBehavior : Behavior<UIElement>
{
public ICommand EnterPressedCommand { get; private set; }
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewKeyDown += EnterPressed;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.PreviewKeyDown -= EnterPressed;
}
private void EnterPressed(object sender, KeyEventArgs keyEventArgs)
{
if (Keyboard.PrimaryDevice != null && Keyboard.PrimaryDevice.ActiveSource != null)
{
var eventArgs = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, keyEventArgs.Key) { RoutedEvent = UIElement.KeyDownEvent };
AssociatedObject.RaiseEvent(eventArgs);
}
}
}
this behavior assigned to datePicker:
<DatePicker x:Name="BirthDateDatePicker" Grid.Column="1"
Grid.Row="6" Margin="3" HorizontalAlignment="Stretch"
IsEnabled="{Binding PersonFieldsEditDenied}"
Validation.ErrorTemplate="{StaticResource DefaultValidationTemplate}"
AutomationProperties.AutomationId="BirthDateDatePicker">
<i:Interaction.Behaviors>
<viewModels:EnterPressedBehavior />
</i:Interaction.Behaviors>
</DatePicker>
which is listened by parent view:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title=""
KeyDown="OnKeyDownHandler">
Code behind:
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// your code
}
}
精彩评论