Reputation: 6149
There is a button which is clicked programmatically with code below but it stays as focused (I am not sure with terminology though). How can I get rid of that?
private void txt_addRemove_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
btn_BC_add.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
}
}
Upvotes: 1
Views: 2071
Reputation: 54562
You could use the TraversalRequest Class
to MoveFocus
or explicitly set your Focus
to another element.
i.e.
btn_BC_add.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
Just in case that your button is last in the Focus order, you can try other FocusNavigationDirection
options such as Previous, First, Last, Left, Right, Up, Down,
.
Upvotes: 0
Reputation: 32515
There are a lot of ways to do what you want properly, here is one:
It's best to set the UpdateSourceTrigger
to PropertyChanged
and use a Command
to bind the Enter key to. This should be the same command that you're Button binds to (so move your business logic to the view-model layer).
Alternatively, if this is the only Button on your form, you can set the IsDefault
property to true (you'll still want to set the UpdateSourceTrigger
to PropertyChanged
).
Upvotes: 0
Reputation: 62265
Apart a fact that really wired to call a ButtonClick
. What you should do, instead, at least define a Command
, associate it to a button, and from this code do not call RaiseEvent
, but call Command
associated to that button. In practise you call a function.
What about a focus, should be enough to set esplicitly a focus to some other control on your view.
Hope this helps.
Upvotes: 2