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

Lam*_*awy 26 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>
Run Code Online (Sandbox Code Playgroud)

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

gle*_*eng 78

您可以通过更改Cursor属性来执行此操作:

<Button Cursor="Hand" .../>
Run Code Online (Sandbox Code Playgroud)

  • 这不适用于visual studio 2013 :( (2认同)
  • 在VS 2013,版本12.0.31101.00 Update 4中,对我来说工作得很好 (2认同)

Bri*_*oll 11

您需要使用Mouse.OverrideCursor

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

myButton.MouseLeave += (s,e) => Mouse.OverrideCursor = Cursors.Arrow;
Run Code Online (Sandbox Code Playgroud)


123*_*9 0 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>
Run Code Online (Sandbox Code Playgroud)


小智 9

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

<Style>
  <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
      <Setter Property="Cursor" Value="Hand"/>
    </Trigger>
  </Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

  • 您需要在样式上设置目标类型才能使其工作。例如`&lt;Style TargetType="{x:Type Button}"&gt;` (2认同)