#680 – IsHitTestVisible Applies to All Child Elements

If you set the IsHitTestVisible  property of a container element to false, it will turn off hit testing for all elements within that container.

Even if a child element sets its own IsHitTestVisible property to true, it will still be hidden from hit testing.  The false value set at the container level applies to all child elements, no matter what value they set.

    <StackPanel IsHitTestVisible="False">
        <Button Content="You can't Click this" HorizontalAlignment="Center" Margin="5" Click="Button_Click_1"/>
        <Button Content="You can't even Click this" HorizontalAlignment="Center" Margin="5" Click="Button_Click_2"
                IsHitTestVisible="True"/>
    </StackPanel>

#679 – Setting IsHitTestVisible to False Prevents Interaction with Controls

When you set the IsHitTestVisible property to false for a user interface element, it will no longer respond to mouse input and will not fire mouse-related events.

With IsHitTestVisible set to false, you therefore can’t use the mouse to interact with a user interface element at all.  In the example below, we can’t use the mouse to give the elements focus or to click or select any of them.

    <StackPanel>
        <Button Content="You can't Click this" HorizontalAlignment="Center" Margin="5" Click="Button_Click_1"
                IsHitTestVisible="False"/>
        <TextBox Text="You can't Edit this" HorizontalAlignment="Center" Margin="5"
                 IsHitTestVisible="False"/>
        <CheckBox Content="You can't Check this" HorizontalAlignment="Center" Margin="5"
                  IsHitTestVisible="False"/>
        <ComboBox HorizontalAlignment="Center" Margin="5" SelectedIndex="0"
                  IsHitTestVisible="False">
            <ComboBox.Items>
                <ComboBoxItem Content="You can't Select this"/>
                <ComboBoxItem Content="Or this"/>
            </ComboBox.Items>
        </ComboBox>
    </StackPanel>


Notice, however, that you can still interact with the elements using the keyboard.  For example, you can tab to the Button and then press Enter to trigger its Click event.

#678 – Hide an Element from the Mouse with IsHitTestVisible Property

You can completely hide a user interface element from the mouse by setting its IsHitTestVisible property to false.  When this property is false, no mouse related events will be fired for that control.

In the example below, one Label has its IsHitTestVisible property set to false.

    <StackPanel>
        <Label Content="Mouse Sees Me" Background="AliceBlue" Margin="10"
               MouseMove="Label_MouseMove_1"/>
        <Label Content="Mouse Doesn't see Me" Background="Beige" Margin="10"
               IsHitTestVisible="False"
               MouseMove="Label_MouseMove_2"/>
    </StackPanel>

Assume that we try to report the mouse position in both event handlers:

        private void ReportMouse(string element, MouseEventArgs e)
        {
            Point p = e.GetPosition(null);
            Console.WriteLine(string.Format("{0} sees mouse at ({1},{2})", element, p.X, p.Y));
        }

        private void Label_MouseMove_1(object sender, MouseEventArgs e)
        {
            ReportMouse("Label 1", e);
        }

        private void Label_MouseMove_2(object sender, MouseEventArgs e)
        {
            ReportMouse("Label 2", e);
        }

When we move the mouse over both labels at runtime, we only see output when it moves over label #1, which does not set IsHitTestVisible to false.