Here is a little hack to get around this problem. The idea is that when Enter gets pressed, and it triggers the button click event, you set the focus to the button. This causes the LostFocus event to trigger the converter. Then you go about your business. The example below uses Caliburn.Micro, but the theory would work in other environments. The one small drawback to coding it this way is that the ViewModel has to know about the control, which makes MVVM purists queasy. I don't think this is a terrible violation of the MVVM model. It is still testable by passing null to the Ok method.
[View File]
<Button x:Name="Ok"
cal:Message.Attach="[Event Click]=[Action Ok($source)]"
Content="OK"
IsDefault="True"
MinWidth="75" />
[ViewModel File]
public void Ok(System.Windows.UIElement button)
{
if (button != null)
{
button.Focus();
}
// other stuff goes here
this.findDateDialog.TryClose(true);
}
The Click action passes the Ok button to the Ok method, which sets the focus to it. If you just click the button with the mouse, then it already has the focus, which is a no-op.
If you know a better way of doing this, let me know in the comments.
No comments :
Post a Comment
Note: Only a member of this blog may post a comment.