当鼠标光标悬停在UWP上时,如何更改文本块的背景颜色?

问题描述 投票:0回答:2

当用户将鼠标光标悬停在我的UWP应用程序中的TextBlock上时,我正在尝试提供反馈。

我想要做的就是在悬停时改变背景颜色,但是我看不出这是怎么回事。

xaml uwp textblock
2个回答
0
投票

在这里,您可以找到TextBox的模板代码:https://msdn.microsoft.com/en-us/library/windows/apps/mt299154.aspx?f=255&MSPPError=-2147217396

定义要更改的行为的相关代码应放在此行之后:

<VisualState x:Name="PointerOver">

基本上你需要使用StoryBoard来设置用于渲染背景的元素的颜色。


0
投票

您不能将背景直接应用于TextBlock,因为它没有BackGround属性,而是您可以将它放在GridBorder中。通过使用PointerEnteredPointerExitedGrid事件,你可以改变TextBlock的背景颜色

<Grid VerticalAlignment="Center" HorizontalAlignment="Left" PointerEntered="Grid_PointerEntered" PointerExited="Grid_PointerExited">
  <TextBlock  Text="Hello"></TextBlock>
</Grid>

private void Grid_PointerEntered(object sender, PointerRoutedEventArgs e)
{
 (sender as Grid).Background = new SolidColorBrush(Colors.Green);
}

private void Grid_PointerExited(object sender, PointerRoutedEventArgs e)
{
 (sender as Grid).Background = new SolidColorBrush(Colors.White);
}

在Ponter Over之前

enter image description here

关于指针

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.