WPF - Trigger elements within a wrap panel

Asked

Viewed 41 times

0

I have a wrap panel that contains several dockpanels, and I need each dockpanel to change its background by hovering over it. I don’t think creating a Rigger for every element would be appropriate. My shampoo is like this:

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

What I got was to get all the child elements using:

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

But I can’t define which object the mouse is on to change it

1 answer

0


I think the most coherent way to do what you want is to create a Rigger

create a Resource style

<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 the style for dockpanels

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

If you are creating dockpanels by code you can take Stylo by Resource.

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

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

But by following its context of taking all the elements that the mouseover is true you can do something similar to that

Children.OfType<DockPanel>().Where(x => x.IsMouseOver == true);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.