[WPF Property Notes] [IsHitTestVisible]

WPF property notes series

Chapter 1 [WPF Property Notes] [IsHitTestVisible]



Preface

Study notes on the WPF property IsHitTestVisible.


1. Introduction to IsHitTestVisble

  • UIElement.IsHitTestVisble property: Gets or sets a value that declares whether this element can be returned as a hit test result for some part of its rendered content. This is a dependency property.
  • IsHitTestVisble property value defaults to true
  • By setting the IsHitTestVisble property value to false, the interface elements can not respond to the mouse and mouse events will not be triggered.
  • By setting the IsHitTestVisble property value to false, you can prohibit all mouse interactions, including clicks on controls, setting input focus to controls through mouse clicks, etc., all of which have no effect.
  • By setting the IsHitTestVisble property value to false, although the mouse is disabled, setting the focus through the tab key and then using the space bar or Enter key to trigger it will respond normally.
  • If a container sets the IsHitTestVisble property value to false, then all child controls in the container will not respond to the mouse. Even if the subspace sets IsHitTestVisble to true, it will be useless.

2. Use of IsHitTestVisble

Set IsHitTestVisble=false, the checkbox can no longer be hit, and clicking it is equivalent to clicking the layer of controls below it, that is, the button, as shown in the following code, click checkbox1, you can see whether there is any change in ✔, and trigger the click of checkbox1 event, but when checkbox2 is clicked, there is no change in ✔ and the click event of checkbox2 is not triggered. Instead, the click event of the button on the lower layer of control is triggered; but it should be noted that if you do not use the mouse to click, but use the tab key to switch focus, Use the space bar or enter to confirm the click, and there will be no difference at this time.

<Grid>
    <Button Content="button" Click="Button_Click"/>
    <StackPanel>
        <WrapPanel>
            <CheckBox IsChecked="True" IsHitTestVisible="True" Click="CheckBox1_Click"/>
            <Button Content="button1" Width="100" Click="Button1_Click"/>
        </WrapPanel>
        <WrapPanel>
            <CheckBox IsChecked="True" IsHitTestVisible="false" Click="CheckBox2_Click"/>
            <Button Content="button2" Width="100"  Click="Button2_Click"/>
        </WrapPanel>
    </StackPanel>
</Grid>
private void CheckBox1_Click(object sender, RoutedEventArgs e)
{
    
    
    MessageBox.Show("CheckBox1");
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
    
    
    MessageBox.Show("Button1");
}
private void CheckBox2_Click(object sender, RoutedEventArgs e)
{
    
    
    MessageBox.Show("CheckBox2");
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
    
    
    MessageBox.Show("Button2");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
    
    
    MessageBox.Show("Button");
}

Insert image description here

3. Application scenarios

  • Basic usage can realize whether to block mouse interaction of specified controls;
  • Implement gradient color processing, such as adding a border with a gradient effect on the upper layer of the control and setting IsHitTestVisble to false, which does not affect the mouse events of the underlying control and can easily implement the gradient effect.
  • To achieve unobstructed display of images, the image is displayed on the top layer. Set IsHitTestVisble to false, so that the entire image can be displayed (putting it on the bottom layer will be partially blocked by the upper-layer controls), and the bottom-layer controls can also be controlled normally;

Summarize

How can you reach a thousand miles without accumulating steps?

Guess you like

Origin blog.csdn.net/Aflashstar/article/details/128789793