Reputation: 1248
I am using a toggle button, and I want it to only change its state due to mouse click. The default behavior is that if the focus is on the button, then the user may press "space" or "enter" to "click" it. I want to cancel this, but to still allow the button to respond to mouse click as usual (by triggering the click event).
any ideas?
Thanks
Upvotes: 1
Views: 691
Reputation: 8232
I tried a Behavior as I suggested in my common on AndrewS's post, but found that the KeyDown event didn't capture the space or enter event. You could capture the event at a higher level and cancel it if it is the space or enter key.
XAML:
<StackPanel KeyUp="sp_KeyUp">
<ToggleButton x:Name="toggleButton" content="toggle me" width="120" height="20"/>
</StackPanel>
Code Behind:
public void sp_KeyUp(object sender, KeyBoardEventArgs e)
{
if(e.Key == System.Windows.Input.Key.Enter || e.Key == System.Windows.Input.Key.Space)
{
e.Handled = true; // this should cancel the event
}
}
Upvotes: 1