当我将光标悬停在按钮上时,将其更改为手动
本文关键字:光标 悬停 按钮 | 更新日期: 2023-09-27 17:58:43
当鼠标悬停在按钮上时,我想将光标更改为指针,例如,我有这个按钮:
<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。
您可以通过更改Cursor
属性来实现这一点:
<Button Cursor="Hand" .../>
按钮需要使用Style
,可以在窗口资源中编写,也可以在按钮样式中编写:
<Style>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
</Style.Triggers>
</Style>
您需要使用Mouse.OverrideCursor
:
myButton.MouseEnter += (s,e) => Mouse.OverrideCursor = Cursors.Hand;
myButton.MouseLeave += (s,e) => Mouse.OverrideCursor = Cursors.Arrow;
使用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>