Santiago Blanco on .Net

Finding solutions to my everyday programming problems

WPF – Execute button command on MouseOver

Posted by Santiago Blanco on December 11, 2009

I am working on a custom WPF ListBox control with auto-scroll functionality and I spent quite some time today trying to find out how to execute the Button command when the mouse is over the button instead of when the click event is raised.

It turns out that the solution is quite simple. The only thing I had to do is set the ClickMode property on the button to “Hover”.

  1. <Style x:Key=listBoxWithAutoScroll TargetType=“{x:Type ListBox} >
  2.   <Setter Property=Template>
  3.     <Setter.Value >
  4.       <ControlTemplate>
  5.         <DockPanel>
  6.           <RepeatButton x:Name=LineLeftButton DockPanel.Dock=Left Width=20
  7.                  Content=&lt;
  8.                  Command=“{x:Static ScrollBar.LineLeftCommand}
  9.                  CommandTarget=“{Binding ElementName=scrollviewer}
  10.                 ClickMode=Hover />
  11.           <RepeatButton x:Name=LineRightButton DockPanel.Dock=Right Width=20
  12.                 Content=&gt;
  13.                 Command=“{x:Static ScrollBar.LineRightCommand}
  14.                 CommandTarget=“{Binding ElementName=scrollviewer}
  15.                 ClickMode=Hover/>
  16.           <Border BorderThickness=1 BorderBrush=Gray Background=White>
  17.             <ScrollViewer x:Name=scrollviewer>
  18.               <ItemsPresenter/>
  19.             </ScrollViewer>
  20.           </Border>
  21.         </DockPanel>
  22.       </ControlTemplate>
  23.     </Setter.Value>
  24.   </Setter>
  25.   <Setter Property=ItemsPanel>
  26.     <Setter.Value >
  27.       <ItemsPanelTemplate>
  28.         <VirtualizingStackPanel Orientation=Horizontal/>
  29.       </ItemsPanelTemplate>
  30.     </Setter.Value>
  31.   </Setter>
  32. </Style>

2 Responses to “WPF – Execute button command on MouseOver”

  1. flow said

    thank you

  2. usefulBee said

    That was awesome. Thanks!

Leave a comment