Lamawy
Lamawy

Reputation: 643

Change cursor to hand when I hover over a button

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.

Upvotes: 55

Views: 67881

Answers (4)

yosef_maj
yosef_maj

Reputation: 271

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>

Upvotes: 27

123 456 789 0
123 456 789 0

Reputation: 10865

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>

Upvotes: 10

Brian Driscoll
Brian Driscoll

Reputation: 19635

You need to use Mouse.OverrideCursor:

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

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

Upvotes: 12

gleng
gleng

Reputation: 6304

You can do this by changing the Cursor property:

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

Upvotes: 142

Related Questions