[wpf] 이름 또는 유형별로 WPF 컨트롤을 찾으려면 어떻게해야합니까?

주어진 이름이나 유형과 일치하는 컨트롤에 대한 WPF 컨트롤 계층을 검색해야합니다. 어떻게해야합니까?



답변

위의 John Myczek과 Tri Q 알고리즘에서 사용하는 템플릿 형식을 결합하여 모든 부모에서 사용할 수있는 findChild 알고리즘을 만들었습니다. 나무를 아래쪽으로 재귀 적으로 검색하면 시간이 오래 걸릴 수 있습니다. WPF 응용 프로그램에서만 이것을 확인했습니다. 발견 할 수있는 오류에 대해서는 의견을 말하고 코드를 수정하겠습니다.

WPF Snoop 은 비주얼 트리를 보는 데 유용한 도구입니다. 테스트하거나이 알고리즘을 사용하여 작업을 확인하는 동안이를 사용하는 것이 좋습니다.

Tri Q의 알고리즘에는 작은 오류가 있습니다. 자식을 찾은 후 childrenCount가 1보다 크면 다시 반복하면 올바르게 찾은 자식을 덮어 쓸 수 있습니다. 따라서이 if (foundChild != null) break;조건을 처리하기 위해 코드에 코드를 추가했습니다 .

/// <summary>
/// Finds a Child of a given item in the visual tree. 
/// </summary>
/// <param name="parent">A direct parent of the queried item.</param>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="childName">x:Name or Name of child. </param>
/// <returns>The first parent item that matches the submitted type parameter. 
/// If not matching item can be found, 
/// a null parent is being returned.</returns>
public static T FindChild<T>(DependencyObject parent, string childName)
   where T : DependencyObject
{
  // Confirm parent and childName are valid. 
  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);
    // If the child is not of the request child type child
    T childType = child as T;
    if (childType == null)
    {
      // recursively drill down the tree
      foundChild = FindChild<T>(child, childName);

      // If the child is found, break so we do not overwrite the found child. 
      if (foundChild != null) break;
    }
    else if (!string.IsNullOrEmpty(childName))
    {
      var frameworkElement = child as FrameworkElement;
      // If the child's name is set for search
      if (frameworkElement != null && frameworkElement.Name == childName)
      {
        // if the child's name is of the request name
        foundChild = (T)child;
        break;
      }
    }
    else
    {
      // child element found.
      foundChild = (T)child;
      break;
    }
  }

  return foundChild;
}

다음과 같이 호출하십시오.

TextBox foundTextBox =
   UIHelper.FindChild<TextBox>(Application.Current.MainWindow, "myTextBoxName");

참고 Application.Current.MainWindow모든 부모 윈도우가 될 수 있습니다.


답변

FrameworkElement.FindName (string)을 사용하여 이름으로 요소를 찾을 수도 있습니다 .

주어진:

<UserControl ...>
    <TextBlock x:Name="myTextBlock" />
</UserControl>

코드 숨김 파일에서 다음을 작성할 수 있습니다.

var myTextBlock = (TextBlock)this.FindName("myTextBlock");

물론 x : Name을 사용하여 정의되었으므로 생성 된 필드를 참조 할 수 있지만 정적으로가 아니라 동적으로 조회하려고 할 수 있습니다.

이 방법은 명명 된 항목이 여러 번 나타나는 (템플릿 사용 당 한 번) 템플릿에도 사용할 수 있습니다.


답변

VisualTreeHelper 를 사용하여 컨트롤을 찾을 수 있습니다. 아래는 VisualTreeHelper를 사용하여 지정된 유형의 부모 컨트롤을 찾는 방법입니다. VisualTreeHelper를 사용하여 다른 방법으로 컨트롤을 찾을 수도 있습니다.

public static class UIHelper
{
   /// <summary>
   /// Finds a parent of a given item on the visual tree.
   /// </summary>
   /// <typeparam name="T">The type of the queried item.</typeparam>
   /// <param name="child">A direct or indirect child of the queried item.</param>
   /// <returns>The first parent item that matches the submitted type parameter. 
   /// If not matching item can be found, a null reference is being returned.</returns>
   public static T FindVisualParent<T>(DependencyObject child)
     where T : DependencyObject
   {
      // get parent item
      DependencyObject parentObject = VisualTreeHelper.GetParent(child);

      // we’ve reached the end of the tree
      if (parentObject == null) return null;

      // check if the parent matches the type we’re looking for
      T parent = parentObject as T;
      if (parent != null)
      {
         return parent;
      }
      else
      {
         // use recursion to proceed with next level
         return FindVisualParent<T>(parentObject);
      }
   }
}

다음과 같이 호출하십시오.

Window owner = UIHelper.FindVisualParent<Window>(myControl);


답변

나는 다른 모든 사람들을 반복 할 수도 있지만 유형과 이름으로 아이를 얻을 수있는 FindChild () 메서드를 사용하여 DependencyObject 클래스를 확장하는 예쁜 코드가 있습니다. 포함하고 사용하십시오.

public static class UIChildFinder
{
    public static DependencyObject FindChild(this DependencyObject reference, string childName, Type childType)
    {
        DependencyObject foundChild = null;
        if (reference != null)
        {
            int childrenCount = VisualTreeHelper.GetChildrenCount(reference);
            for (int i = 0; i < childrenCount; i++)
            {
                var child = VisualTreeHelper.GetChild(reference, i);
                // If the child is not of the request child type child
                if (child.GetType() != childType)
                {
                    // recursively drill down the tree
                    foundChild = FindChild(child, childName, childType);
                }
                else if (!string.IsNullOrEmpty(childName))
                {
                    var frameworkElement = child as FrameworkElement;
                    // If the child's name is set for search
                    if (frameworkElement != null && frameworkElement.Name == childName)
                    {
                        // if the child's name is of the request name
                        foundChild = child;
                        break;
                    }
                }
                else
                {
                    // child element found.
                    foundChild = child;
                    break;
                }
            }
        }
        return foundChild;
    }
}

도움이 되길 바랍니다.


답변

코드에 대한 내 확장.

  • 유형, 유형 및 기준 (조건 자)별로 하나의 하위 항목을 찾고, 기준을 충족하는 유형의 모든 하위 항목을 찾기위한 과부하를 추가했습니다.
  • FindChildren 메서드는 DependencyObject의 확장 메서드 일뿐 아니라 반복자입니다.
  • FindChildren도 논리 하위 트리를 걷습니다. 블로그 게시물에 링크 된 Josh Smith의 게시물을 참조하십시오.

출처 :
https://code.google.com/p/gishu-util/source/browse/#git%2FWPF%2F 유틸리티

설명 블로그 게시물 :
http://madcoderspeak.blogspot.com/2010/04/wpf-find-child-control-of-specific-type.html


답변

특정 유형의 모든 컨트롤을 찾으려면이 스 니펫에도 관심이있을 수 있습니다.

    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject parent)
        where T : DependencyObject
    {
        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);

            var childType = child as T;
            if (childType != null)
            {
                yield return (T)child;
            }

            foreach (var other in FindVisualChildren<T>(child))
            {
                yield return other;
            }
        }
    }


답변

이것은 일부 요소를 닫을 것입니다-더 넓은 범위의 컨트롤을 지원하려면 다음과 같이 확장해야합니다. 간단한 토론은 여기를보십시오

 /// <summary>
 /// Helper methods for UI-related tasks.
 /// </summary>
 public static class UIHelper
 {
   /// <summary>
   /// Finds a parent of a given item on the visual tree.
   /// </summary>
   /// <typeparam name="T">The type of the queried item.</typeparam>
   /// <param name="child">A direct or indirect child of the
   /// queried item.</param>
   /// <returns>The first parent item that matches the submitted
   /// type parameter. If not matching item can be found, a null
   /// reference is being returned.</returns>
   public static T TryFindParent<T>(DependencyObject child)
     where T : DependencyObject
   {
     //get parent item
     DependencyObject parentObject = GetParentObject(child);

     //we've reached the end of the tree
     if (parentObject == null) return null;

     //check if the parent matches the type we're looking for
     T parent = parentObject as T;
     if (parent != null)
     {
       return parent;
     }
     else
     {
       //use recursion to proceed with next level
       return TryFindParent<T>(parentObject);
     }
   }

   /// <summary>
   /// This method is an alternative to WPF's
   /// <see cref="VisualTreeHelper.GetParent"/> method, which also
   /// supports content elements. Do note, that for content element,
   /// this method falls back to the logical tree of the element!
   /// </summary>
   /// <param name="child">The item to be processed.</param>
   /// <returns>The submitted item's parent, if available. Otherwise
   /// null.</returns>
   public static DependencyObject GetParentObject(DependencyObject child)
   {
     if (child == null) return null;
     ContentElement contentElement = child as ContentElement;

     if (contentElement != null)
     {
       DependencyObject parent = ContentOperations.GetParent(contentElement);
       if (parent != null) return parent;

       FrameworkContentElement fce = contentElement as FrameworkContentElement;
       return fce != null ? fce.Parent : null;
     }

     //if it's not a ContentElement, rely on VisualTreeHelper
     return VisualTreeHelper.GetParent(child);
   }
}