【问题标题】:Change cursor to hand when I hover over a button当我将鼠标悬停在按钮上时将光标更改为手
【发布时间】:2014-05-31 19:46:26
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

    您可以通过更改Cursor 属性来做到这一点:

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

    【讨论】:

    • 在 VS 2013 版本 12.0.31101.00 更新 4 中对我来说很好用
    • 完美答案。
    • 很好,但它可以(应该)以样式完成。如果我想在所有按钮上使用这个怎么办。
    【解决方案2】:

    你需要使用Style作为按钮,你可以写在窗口资源或按钮的样式:

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

    【讨论】:

    • 您需要在样式上设置目标类型才能使其正常工作。例如&lt;Style TargetType="{x:Type Button}"&gt;
    【解决方案3】:

    你需要使用Mouse.OverrideCursor:

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

    【讨论】:

    • 出于某种原因,这是要走的路……非常感谢!
    【解决方案4】:

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

    【讨论】:

    • 我真的很抱歉,但我之前没有使用触发器..你能解释一下如何通过触发器来做吗?
    • @user3542555 抱歉,没看到是 WinRT。更新了我的答案。使用VSM
    • 不,没关系.. 好的,那么我在哪里将我的按钮模板放在你的代码中?
    • &lt;Button.Template&gt;VisualStateManager 之前或之后
    猜你喜欢
    • 2016-01-22
    • 2022-01-26
    • 2021-11-27
    • 1970-01-01
    • 2012-09-22
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    相关资源
    最近更新 更多