51

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Lamawy
  • 603
  • 1
  • 7
  • 12

4 Answers4

134

You can do this by changing the Cursor property:

<Button Cursor="Hand" .../>
gleng
  • 6,185
  • 4
  • 21
  • 35
21

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>
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
yosef_maj
  • 211
  • 2
  • 2
12

You need to use Mouse.OverrideCursor:

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

myButton.MouseLeave += (s,e) => Mouse.OverrideCursor = Cursors.Arrow;
Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65
9

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>
123 456 789 0
  • 10,565
  • 4
  • 43
  • 72