将鼠标悬停在按钮上时,将光标更改为手形

时间:2014-04-16 18:56:10

标签: c# button cursor visual-studio-2013 windows-8.1

我想将鼠标悬停在按钮上时将光标更改为手,例如,我有这个按钮:

<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>

当我将鼠标悬停在按钮上时,如何更改光标?我使用Visual Studio 2013 for Windows Store 8和C#-XAML。

4 个答案:

答案 0 :(得分:70)

您可以通过更改Cursor属性:

来执行此操作
<Button Cursor="Hand" .../>

答案 1 :(得分:9)

使用Visual State Manager

将您的XAML更新为此

<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>

答案 2 :(得分:9)

您需要使用Style按钮,是否可以使用窗口资源或按钮的样式进行编写:

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

答案 3 :(得分:4)

您需要使用Mouse.OverrideCursor

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

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