当鼠标指针悬停在UWP上时,如何更改TextBlock中的背景颜色?

时间:2019-03-03 06:48:01

标签: xaml uwp textblock

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

我要做的就是在悬停时更改背景颜色,但是我看不到这是怎么可能的。

2 个答案:

答案 0 :(得分:0)

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

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

n

基本上,您需要使用<VisualState x:Name="PointerOver"> 来设置用于渲染背景的元素的颜色。

答案 1 :(得分:0)

您不能直接将背景应用于TextBlock,因为它没有BackGround属性,而是可以将其放在GridBorder中。通过使用PointerEntered PointerExited Grid 事件,您可以更改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结束之前

enter image description here

在指针上方

enter image description here