WPF - 如何在父母鼠标悬停时改变孩子的风格 [英] WPF - How to change children's style on mouseover of parent
问题描述
我有一个 StackPanel
(1),里面有另一个 StackPanel
(2).
I have a StackPanel
(1), with another StackPanel
(2) inside.
SP 2 应该隐藏(不透明度:0),直到 SP 1 悬停.鼠标悬停应该将 SP2 的样式更改为 Opacity:100.
SP 2 should be hidden (Opacity:0) until SP 1 is hovered. The mouse-over should change the style of SP2 to Opacity:100.
我已经尝试在 StackPanel 资源中定义样式,并在那里使用触发器来定位内部面板,但我不确定我应该如何从触发器内部定位子项.
I've tried defining styles in the StackPanel resources, and using triggers there to then target the inside panel, but I'm not sure how I should be targeting the children from inside the trigger.
一个简单的样式结构是什么?
What would be a simple style structure to do this?
推荐答案
我不完全了解您的需求,所以我发布了 2 个示例.
I do not fully understand what you need so i posted 2 samples.
为了清晰起见,使用颜色进行采样:
Sample with colors for clarity :
1) 当我们将鼠标悬停在 sp1 sp2 上时颜色变为绿色
1) when we have mouseover on sp1 sp2 getting green color
<Window x:Class="Prognoz.GP.DataCollection.TestMarkupProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Window.Resources>
<Style x:Key="test" TargetType="StackPanel">
<Setter Property="Background" Value="Red" />
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=StackPanel,AncestorLevel=1}, Path=IsMouseOver}" Value="True" >
<Setter Property="Background" Value="Green" />
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<StackPanel Width="400" Height="400" Background="Yellow">
<StackPanel Width="350" Height="350" Style="{StaticResource test}"/>
</StackPanel>
</Grid>
</Window>
2) 当我们将鼠标悬停在 sp2 sp2 上时颜色变为绿色
2) when we have mouseover on sp2 sp2 getting green color
<Style x:Key="test" TargetType="StackPanel">
<Setter Property="Background" Value="Red" />
<Style.Triggers>
<Trigger Property="StackPanel.IsMouseOver" Value="True" >
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
这篇关于WPF - 如何在父母鼠标悬停时改变孩子的风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!