Change cursor to hand when I hover over a button

53,220

Solution 1

You can do this by changing the Cursor property:

<Button Cursor="Hand" .../>

Solution 2

You need to use Style for buttons, could you write in window resource or in button's style:

<Style>
  <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
      <Setter Property="Cursor" Value="Hand"/>
    </Trigger>
  </Style.Triggers>
</Style>

Solution 3

You need to use Mouse.OverrideCursor:

myButton.MouseEnter += (s,e) => Mouse.OverrideCursor = Cursors.Hand;

myButton.MouseLeave += (s,e) => Mouse.OverrideCursor = Cursors.Arrow;

Solution 4

Use Visual State Manager

Update your XAML to be like this

<Button Content="Beh}"  Style="{StaticResource ButtonHover}">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal"/>
                <VisualState x:Name="MouseOver">
                    <Storyboard>
                    <ObjectAnimationUsingKeyFrames  Storyboard.TargetProperty="(FrameworkElement.Cursor)">
                        <DiscreteObjectKeyFrame KeyTime="00:00:00">
                            <DiscreteObjectKeyFrame.Value>
                                <Cursor>Hand</Cursor>
                            </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Button>
Share:
53,220
Lamawy
Author by

Lamawy

Updated on July 09, 2022

Comments

  • Lamawy
    Lamawy almost 2 years

    I want to change the cursor to hand when hovering over a button, for example, I have this button :

    <Button Content="" HorizontalAlignment="Left" Margin="229,128,0,0" VerticalAlignment="Top" Height="107" Width="170" Grid.RowSpan="2">
         <Button.Template>
             <ControlTemplate TargetType="Button">
                 <Grid>
                     <Grid.Background>
                         <ImageBrush ImageSource="africa/picture17.png"/>
                     </Grid.Background>
                     <ContentPresenter/>
                 </Grid>
             </ControlTemplate>
         </Button.Template>
    </Button>
    

    How to change the cursor to hand when I hover over the button? I'm using Visual Studio 2013 for Windows Store 8 and C#-XAML.

  • Lamawy
    Lamawy about 10 years
    i'm really sorry but i didn't use triggers before .. can you please explain more how to do it by triggers ?
  • 123 456 789 0
    123 456 789 0 about 10 years
    @user3542555 Sorry, didn't see it was WinRT. Updated my answer. Use VSM
  • Lamawy
    Lamawy about 10 years
    no it is ok .. ok then where do i put my button template inside your code ?
  • 123 456 789 0
    123 456 789 0 about 10 years
    <Button.Template> Either before VisualStateManager or after
  • Jagd
    Jagd over 8 years
    Works just fine for me in VS 2013, version 12.0.31101.00 Update 4
  • Adam H
    Adam H almost 6 years
    You'll need to set the target type on the style for this to work. eg <Style TargetType="{x:Type Button}">
  • Shariful Islam Mubin
    Shariful Islam Mubin over 4 years
    Perfect answer.
  • nrod
    nrod about 4 years
    By some reason, this was the way to go... so thank you very much!
  • ed22
    ed22 almost 3 years
    Great, but it could (should) be done in styles. What if I want this on al the buttons.