Kobi Hari
Kobi Hari

Reputation: 1248

How to make a silverlight button respond only to mouse

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

Answers (2)

AlignedDev
AlignedDev

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

AndrewS
AndrewS

Reputation: 6094

You could set the IsTabStop property to false in which case you won't be able to focus the button and therefore it won't be able to respond to the keyboard.

Upvotes: 2

Related Questions