WPF: user panel visibility

I have WPF user control (myGraphicControl) in a tab (WPF application).

When the size of the form changes, I redraw the chart in myGraphicControl.

Since the operation is redrawing - I only need to do this control on the tab.

How can a WPF (user) control determine if it is really β€œvisible”?

PS.

by Visible I mean that the user can see this. let's say if Visible TextBox is on the current invisible tab, this text block is not displayed by the user.

+4
source share
3 answers

I don't believe there is a quick fix here, but you can do something using UIElement.InputHitTest(Point) .

You can make a call similar to

 //get the coordinates of the top left corner of the child control within //the parent var childTopLeft = childControl.TranslatePoint(new Point(), parentControl); //check whether or not the child control is returned when you request the element //at that coordinate through hit testing var isVisible = (parentControl.InputHitTest(childTopLeft) == childControl); 

However, I must point out that I have not tried this myself and that it probably will not work in the following scenarios:

  • Transparent elements - as a rule, transparent backgrounds cause testing when testing a control for a parent
  • Partially closed items - you can only check one point at a time, so if you see only part of your child control, you will need to check the correct point.
+3
source

I found that although Steve's method usually works, it works much more reliably if you get a point from somewhere in the middle of the child control. I suggest that perhaps the layout, somewhere nearby, makes the InputHitTest check somewhat inaccurate. So, change your first line to the next and you are golden:

 var childTopLeft = childControl.TranslatePoint(new Point(childControl.RenderSize.Width/2, childControl.RenderSize.Height/2), parentControl); 
+2
source

Perhaps UIElement.IsVisible would be helpful? It works well for tab content. In any case, you can use the solution described here .

I have one more solution. The current TabControl implementation removes inactive tabs from the visual tree. So, another way to determine if your element is visible is to find PresentationSource. It will be empty for inactive tab items.

+1
source

Source: https://habr.com/ru/post/1346918/


All Articles