WPF 이름별 모/자 컨트롤 찾기

1860 단어 wpf
VisualTreeHelper의 GetChildrenCount 및 GetChild 방법을 사용하여 객체의 하위 컨트롤을 찾고 GetParent 방법을 사용하여 객체의 상위 레벨을 찾습니다.
public T FindChild(DependencyObject parent, string childName)where T : DependencyObject
        {
            if (parent == null) return null;

            T foundChild = null;

            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                //  
                T childType = child as T;
                if (childType == null)
                {
                    //  
                    foundChild = FindChild(child, childName);

                    //   
                    if (foundChild != null) break;
                }
                else if (!string.IsNullOrEmpty(childName))
                {
                    var frameworkElement = child as FrameworkElement;
                    //  
                    if (frameworkElement != null && frameworkElement.Name == childName)
                    {
                        foundChild = (T) child;
                        break;
                    }
                    //  
                    foundChild = FindChild(child, childName);

                    //   
                    if (foundChild != null) break;
                }
                else
                {
                    //  
                    foundChild = (T)child;
                    break;
                }
            }

            return foundChild;
        }
12         DependencyObject parent = VisualTreeHelper.GetParent(obj);

좋은 웹페이지 즐겨찾기