Hit-testing on entire area of custom FrameworkElement that contains transparent elements

StackOverflow https://stackoverflow.com/questions/10890119

  •  12-06-2021
  •  | 
  •  

質問

I've got a custom control that inherits from FrameworkElement. It contains a Visual that contains transparent areas. I'm trying to make the entire control area respond to hit tests, but at the moment, when I click on part of the control that displays a transparent area of the Visual, the click passes through to the underlying layer.

Is there a way of making the entire control hit-testable without using a hack?

I'd like to stay away from techniques like painting the background of the Visual white, or adding a Border around the custom control that has the same event handler set.

Thanks in advance!

役に立ちましたか?

解決

Simply use a transparent brush and it will respond to hit testing.

For example, if you have null background brush, then hit testing will pass right through.

If you use Brushes.Transparent as a background or fill area, then it will work with hit testing.

他のヒント

using Background or Fill = "{x:Null}" which is the default for some classes like Grid is not the same as Background or Fill = "Transparent". Setting Transparent as the background/fill color should work.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top