Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - How to change children's style on mouseover of parent

Tags:

wpf

xaml

I have a StackPanel (1), with another StackPanel (2) inside.

SP 2 should be hidden (Opacity:0) until SP 1 is hovered. The mouse-over should change the style of SP2 to Opacity:100.

enter image description here

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?

like image 739
Yisela Avatar asked Apr 15 '13 04:04

Yisela


1 Answers

I do not fully understand what you need so i posted 2 samples.

Sample with colors for clarity :

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) 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>
like image 179
Frank59 Avatar answered Sep 27 '22 02:09

Frank59