Simple WPF Tip : Is A in bound of B?

프로그래밍 2010.03.29 10:37 Posted by 아일레프

이 포스트는 Is it possible to find out if an Item is currently hidden because it exceeds the bounds of real estate given to it? 의 내 답글을 옮긴 것이다.


Q : As the title states, I'm wondering if it's possible to determine if a particular UI element is currently hidden because it's out of bounds.  For example, if an item is located within a ScrollViewer, and it is below the scrollable region currently visible, can you query that somehow?


My Answer :

Visual 객체의 TransformToVisual 메소드를 이용해 다음과 같이 Bound 여부를 알아 낼 수 있겠다.
From” 객체와 “To” 객체의 2차원 공간에서의 Rectangle을 구하고, Rectangle Intersect의 존재여부를 검사한다. 만약 Intersect가 존재한다면 “To is in bound of From”인 것이다.


public static bool IsInBound(this FrameworkElement from, FrameworkElement to)


            MatrixTransform transform = to.TransformToVisual(from) as MatrixTransform;


            Rect rect = Rect.Intersect(new Rect(new Point(0, 0),

                                                        new Point(from.ActualWidth, from.ActualHeight)),

                                            new Rect(new Point(transform.Value.OffsetX, transform.Value.OffsetY),

                                                        new Point(to.ActualWidth + transform.Value.OffsetX,

to.ActualHeight + transform.Value.OffsetY)));


            if (rect.Equals(Rect.Empty))


                return false;




                return true;




혹시나 편한 Helper가 이미 존재한다면 GG