WPF - Trigger on elements inside a wrap panel

0

I have a wrap panel that contains several dockpanels, and I need each dockpanel to change its background by hovering the mouse over it. I think creating a trigger for each element would not be appropriate. My sham is like this:

<StackPanel>
    <WrapPanel>
        <DockPanel> <!-- Aqui eu tenho muitos DockPanels -->
        </DockPanel>
    </WrapPanel
</StackPanel>

What I was able to do was fetch all child elements using:

Children.OfType<DockPanel>().Any();

But I can not define which object the mouse is on top to change it

    
asked by anonymous 25.09.2018 / 15:23

1 answer

0

I think the most consistent way to do what you want is to create a trigger

create a style resource

<Style x:Key="MeuMauseOver" TargetType="DockPanel">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Pink"></Setter>
        </Trigger>
        <Trigger Property="IsMouseOver" Value="False">
            <Setter Property="Background" Value="blue"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

And set style for dockpanels

<StackPanel>
    <WrapPanel>
        <DockPanel Style="{StaticResource MeuMauseOver}">
        </DockPanel>
    </WrapPanel
</StackPanel>

If you are creating the dockpanels by the code you can take the stylo by Resource.

Style MeuMauseOver = this.FindResource("MeuMauseOver") as Style;

Where this needs to be a Window element. in case your MainWindow.

But following your context to get all the elements that the mouseover equals true you can do something similar to that

Children.OfType<DockPanel>().Where(x => x.IsMouseOver == true);
    
25.09.2018 / 16:25