Search code examples
wpfdockpanel

IsMouseOver trigger for DockPanel not firing unless children are hovered over


This question has been asked a couple of times before, but at this time, none of the answers I've found turned out to be working.

So I have a custom DockPanel with 2 child elements inside, 1 TextBox and 1 Image. I intended to have the DockPanel change color on hover, so I put a Trigger inside of it. But currently, it only changes color if I hover over the children, whereas a hover over whitespace between elements doesn't do anything.

I've read similar posts which mentioned using isHitTestVisible, but it doesn't do anything.

Short version of my code is the following:

<DockPanel>

    <!-- Change color on mouse hover -->
    <DockPanel.Style>
        <Style>
            <Style.Triggers>
                 <Trigger Property="DockPanel.IsMouseOver" Value="True">
                       <Setter Property="DockPanel.Background" Value="#FFe6ffe6"/>
                 </Trigger>
            </Style.Triggers>
        </Style>
    </DockPanel.Style>

    <StackPanel Orientation="Vertical" DockPanel.Dock="Center">
        <!-- TextBox and Image code here -->
    </StackPanel>
</DockPanel>

Solution

  • Add this to your dock panel style.

    <Trigger Property="DockPanel.IsMouseOver" Value="False">
          <Setter Property="DockPanel.Background" Value="Transparent"/>
    </Trigger>