【发布时间】:2011-12-01 18:47:51
【问题描述】:
假设我有这段代码:
<Window>
<Window.Resources>
<Color x:Key="MyColor"
A="255"
R="152"
G="152"
B="152" />
<DropShadowEffect x:Key="MyEffect"
ShadowDepth="0"
Color="{StaticResource MyColor}"
BlurRadius="10" />
<Style x:Key="MyGridStyle"
TargetType="{x:Type Grid}">
<Setter Property="Height"
Value="200" />
<Setter Property="Width"
Value="200" />
<Style.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Width"
Value="100" />
</Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Height"
Value="100" />
<Setter Property="Width"
Value="100" />
</Style>
</Style.Resources>
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="true">
<!-- How do I apply my effect when this grid is hovered over to Image and TextBox, but not the grid itself? -->
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid Style="{StaticResource MyGridStyle}">
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image Grid.Row="0"
Grid.Column="0"
Source="image.png" />
<TextBlock Grid.Row="0"
Grid.Column="0"
Text="Hover Over Me" />
</Grid>
</Window>
基本上我有一个样式应用于网格,它表示其中的任何文本块或图像都应该是特定大小的样式。
我想在 Grid 上创建一个触发器,使效果应用于 Grid 内的所有 TextBlocks 和图像,但不应用于 Grid 本身。
我可以将 Trigger 直接应用于 TextBlock 和/或 Image,但效果只发生在每个元素上。无论我悬停在哪个内部子元素上,我都需要对 Grid 中的任何 TextBlock 和/或 Image 产生效果。
谁能帮我解决这个问题?
【问题讨论】:
标签: wpf triggers mouseover effect childcontrol