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”.
- <Style x:Key=“listBoxWithAutoScroll“ TargetType=“{x:Type ListBox}“ >
- <Setter Property=“Template“>
- <Setter.Value >
- <ControlTemplate>
- <DockPanel>
- <RepeatButton x:Name=“LineLeftButton“ DockPanel.Dock=“Left“ Width=“20“
- Content=“<“
- Command=“{x:Static ScrollBar.LineLeftCommand}“
- CommandTarget=“{Binding ElementName=scrollviewer}“
- ClickMode=“Hover“ />
- <RepeatButton x:Name=“LineRightButton“ DockPanel.Dock=“Right“ Width=“20“
- Content=“>“
- Command=“{x:Static ScrollBar.LineRightCommand}“
- CommandTarget=“{Binding ElementName=scrollviewer}“
- ClickMode=“Hover“/>
- <Border BorderThickness=“1“ BorderBrush=“Gray“ Background=“White“>
- <ScrollViewer x:Name=“scrollviewer“>
- <ItemsPresenter/>
- </ScrollViewer>
- </Border>
- </DockPanel>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- <Setter Property=“ItemsPanel“>
- <Setter.Value >
- <ItemsPanelTemplate>
- <VirtualizingStackPanel Orientation=“Horizontal“/>
- </ItemsPanelTemplate>
- </Setter.Value>
- </Setter>
- </Style>
flow said
thank you
usefulBee said
That was awesome. Thanks!