C# 如何在UWP应用程序中悬停时更改gridview itemtemplate中texblock的颜色?
我已经创建了一个C# 如何在UWP应用程序中悬停时更改gridview itemtemplate中texblock的颜色?,c#,visual-studio,xaml,gridview,uwp,C#,Visual Studio,Xaml,Gridview,Uwp,我已经创建了一个gridview,如下图所示。其中每个项目由一个堆栈面板组成,其中包含和图像以及一个文本块。我想更改gridviewitem的背景色以及gridviewitem悬停时的textblock 我已通过使用gridviewtempresenter中的PointerOverBackground更改了gridview项的背景色。 如何更改悬停时文本块的颜色 我已经尝试过这段代码,但我相信会有更好的方法来实现这一点 private void StackPanel_PointerEnter
gridview
,如下图所示。其中每个项目由一个堆栈面板
组成,其中包含和图像以及一个文本块。我想更改gridviewitem
的背景色以及gridviewitem
悬停时的textblock
我已通过使用gridviewtempresenter
中的PointerOverBackground
更改了gridview
项的背景色。
如何更改悬停时文本块的颜色
我已经尝试过这段代码,但我相信会有更好的方法来实现这一点
private void StackPanel_PointerEntered(object sender, PointerRoutedEventArgs e)
{
StackPanel sp = (StackPanel)sender;
foreach (var item in sp.Children)
{
if (item.GetType().Equals(typeof(TextBlock)))
{
TextBlock tb = item as TextBlock;
brush = new SolidColorBrush();
brush.Color = Color.FromArgb(0, 0, 0, 0);
tb.Foreground = brush;
}
}
}
如何在UWP应用程序中悬停时更改gridview itemtemplate中texblock的颜色
对于您的需求,更好的方法是使用
在DataTempate
中编辑属性。您可以使用EventTriggerBehavior
检测指针悬停事件,然后编辑textblock的属性。有关详细信息,请参考以下代码
<DataTemplate>
<Grid Name="GridPanel">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="InfoTextBlock" Text="{Binding}" />
<TextBlock
x:Name="FlagTextBlock"
Grid.Column="1"
Text="Hover"
Visibility="Collapsed"
>
<Interactivity:Interaction.Behaviors>
<Interactions:EventTriggerBehavior EventName="PointerEntered" SourceObject="{Binding ElementName=GridPanel}">
<Interactions:ChangePropertyAction
PropertyName="Visibility"
TargetObject="{Binding ElementName=FlagTextBlock}"
Value="Visible"
/>
<Interactions:ChangePropertyAction
PropertyName="Foreground"
TargetObject="{Binding ElementName=InfoTextBlock}"
Value="Red"
/>
</Interactions:EventTriggerBehavior>
<Interactions:EventTriggerBehavior EventName="PointerExited" SourceObject="{Binding ElementName=GridPanel}">
<Interactions:ChangePropertyAction
PropertyName="Visibility"
TargetObject="{Binding ElementName=FlagTextBlock}"
Value="Collapsed"
/>
<Interactions:ChangePropertyAction
PropertyName="Foreground"
TargetObject="{Binding ElementName=InfoTextBlock}"
Value="Black"
/>
</Interactions:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</TextBlock>
</Grid>
</DataTemplate>
工作正常!!谢谢你的回答:)嗨,我只是想把这个问题扩大一点PointerEntered
和PointerExited
工作正常,但如何管理PointerPressed
事件。我想在按下指针时更改文本颜色。但问题是,PointerExited
仍在工作,这会再次更改文本颜色。如何处理这个案件?