tags:

views:

291

answers:

4

I am trying to call a command when my mouse is over a toggle button.

I have the following code.

<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Hand"></Setter>
<Setter Property="Command" Value="{Binding Path=PushPinMouse}" />
</Trigger>

When I roll the mouse over, the hand shows. But when i roll the mouse over it doesn't hit my PushPinMouse method.. Why's that?

A: 

Commands are executed by controls like ToggleButton when they are clicked. I don't have an idea about your scenario, but may be you can set IsChecked property in your setter, and have a property binded to IsChecked. Hope it does help.

Trainee4Life
A: 

Commands are not Binded with, you need to assign command use Command=PushPinMouse

viky
A: 

You are changing the command which the button will trigger, but it doesn't actually execute the button until you click it. So in this case you would have to have the mouse over and click to execute the command.

I am assuming also that you have bound your command to the command function using a CommandBinding?

Guy
A: 

What you need here is attached behavior. I can recommend you to see this nice article : http://blogs.microsoft.co.il/blogs/tomershamam/archive/2009/04/14/wpf-commands-everywhere.aspx

Your code would looks like (with the Attached Commands library) :

<Style>
  <Setter Property="ts:CommandSource.Trigger">
    <Setter.Value>
      <ts:PropertyCommandTrigger Property="IsMouseOver" Value="true" Command="{Binding Path=PushPinMouse}"/>
    </Setter.Value>
  </Setter>
</Style>

This says : "When Mouse is over, executes the PushPinMouse command". If this is not the behavior that you need, maybe you can adapt this code ;) Like the others said, a button command is only executed when it is clicked, BUT this library can add commands to other events (whereas routed events or property changed events).

You will still need this trigger :

<Trigger Property="IsMouseOver" Value="True">
  <Setter Property="Cursor" Value="Hand"></Setter>
</Trigger>
Aurélien Ribon