이름 또는 유형별로 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(문자열).
지정:
<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);
다른 모든 사람에게 반복하고 있을 뿐이지만 DependencyObject 클래스를 확장하기 위한 예쁜 코드를 가지고 있습니다.FindChild() 메서드를 사용하면 유형과 이름으로 아이를 얻을 수 있습니다.포함해서 사용하세요.
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);
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 = child;
break;
}
}
else
{
// child element found.
foundChild = child;
break;
}
}
}
return foundChild;
}
}
유용하게 쓰시길 바랍니다.
특정 유형의 모든 컨트롤을 찾으려면 이 스니펫에 관심이 있을 수 있습니다.
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;
}
}
}
코드에 대한 내 확장자.
- 유형별, 유형별 및 기준(예약)별로 한 개의 하위 항목을 찾기 위해 오버로드가 추가됨, 기준을 충족하는 유형의 하위 항목을 모두 찾습니다.
- FindChildren 메서드는 DependencyObject 확장 메서드일 뿐만 아니라 반복 메서드입니다.
- Find Children은 논리 서브트리도 처리합니다.블로그 포스트에 링크된 Josh Smith의 게시물을 참조하십시오.
출처 : https://code.google.com/p/gishu-util/source/browse/ #git%2FWPF%2FUtilities
설명 블로그 투고 : http://madcoderspeak.blogspot.com/2010/04/wpf-find-child-control-of-specific-type.html
이렇게 하면 일부 요소가 해제됩니다. 보다 광범위한 컨트롤을 지원하려면 이렇게 확장해야 합니다.간단한 논의를 위해 여기를 참조하십시오.
/// <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);
}
}
CrimsonX의 코드는 슈퍼클래스 타입에서 동작하지 않기 때문에 편집했습니다.
public static T FindChild<T>(DependencyObject depObj, string childName)
where T : DependencyObject
{
// Confirm obj is valid.
if (depObj == null) return null;
// success case
if (depObj is T && ((FrameworkElement)depObj).Name == childName)
return depObj as T;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
//DFS
T obj = FindChild<T>(child, childName);
if (obj != null)
return obj;
}
return null;
}
저는 일반적으로 재귀적인 것을 좋아하지만, C#에서 프로그래밍할 때는 반복만큼 효율적이지 않기 때문에 다음 솔루션이 John Myczek이 제안한 솔루션보다 더 깔끔하지 않을까요?특정 유형의 상위 컨트롤을 찾기 위해 지정된 컨트롤에서 계층을 검색합니다.
public static T FindVisualAncestorOfType<T>(this DependencyObject Elt)
where T : DependencyObject
{
for (DependencyObject parent = VisualTreeHelper.GetParent(Elt);
parent != null; parent = VisualTreeHelper.GetParent(parent))
{
T result = parent as T;
if (result != null)
return result;
}
return null;
}
'아예'를 수 있어요.Window
라고 하는 제어가 포함되어 있다.ExampleTextBox
:
Window window = ExampleTextBox.FindVisualAncestorOfType<Window>();
계층으로 이동하는 정도를 제어하면서 유형별로 컨트롤을 찾는 코드입니다(maxDepth == 0은 무한히 깊음을 의미).
public static class FrameworkElementExtension
{
public static object[] FindControls(
this FrameworkElement f, Type childType, int maxDepth)
{
return RecursiveFindControls(f, childType, 1, maxDepth);
}
private static object[] RecursiveFindControls(
object o, Type childType, int depth, int maxDepth = 0)
{
List<object> list = new List<object>();
var attrs = o.GetType()
.GetCustomAttributes(typeof(ContentPropertyAttribute), true);
if (attrs != null && attrs.Length > 0)
{
string childrenProperty = (attrs[0] as ContentPropertyAttribute).Name;
foreach (var c in (IEnumerable)o.GetType()
.GetProperty(childrenProperty).GetValue(o, null))
{
if (c.GetType().FullName == childType.FullName)
list.Add(c);
if (maxDepth == 0 || depth < maxDepth)
list.AddRange(RecursiveFindControls(
c, childType, depth + 1, maxDepth));
}
}
return list.ToArray();
}
}
exiton80...사용자 컨트롤을 통해 코드가 반복되지 않는 데 문제가 있었습니다.그것은 그리드 루트에 부딪혀 오류를 던지고 있었다.이것으로 해결할 수 있을 것 같습니다.
public static object[] FindControls(this FrameworkElement f, Type childType, int maxDepth)
{
return RecursiveFindControls(f, childType, 1, maxDepth);
}
private static object[] RecursiveFindControls(object o, Type childType, int depth, int maxDepth = 0)
{
List<object> list = new List<object>();
var attrs = o.GetType().GetCustomAttributes(typeof(ContentPropertyAttribute), true);
if (attrs != null && attrs.Length > 0)
{
string childrenProperty = (attrs[0] as ContentPropertyAttribute).Name;
if (String.Equals(childrenProperty, "Content") || String.Equals(childrenProperty, "Children"))
{
var collection = o.GetType().GetProperty(childrenProperty).GetValue(o, null);
if (collection is System.Windows.Controls.UIElementCollection) // snelson 6/6/11
{
foreach (var c in (IEnumerable)collection)
{
if (c.GetType().FullName == childType.FullName)
list.Add(c);
if (maxDepth == 0 || depth < maxDepth)
list.AddRange(RecursiveFindControls(
c, childType, depth + 1, maxDepth));
}
}
else if (collection != null && collection.GetType().BaseType.Name == "Panel") // snelson 6/6/11; added because was skipping control (e.g., System.Windows.Controls.Grid)
{
if (maxDepth == 0 || depth < maxDepth)
list.AddRange(RecursiveFindControls(
collection, childType, depth + 1, maxDepth));
}
}
}
return list.ToArray();
}
다음과 같은 시퀀스 함수가 있습니다(완전히 일반적입니다).
public static IEnumerable<T> SelectAllRecursively<T>(this IEnumerable<T> items, Func<T, IEnumerable<T>> func)
{
return (items ?? Enumerable.Empty<T>()).SelectMany(o => new[] { o }.Concat(SelectAllRecursively(func(o), func)));
}
직계 자녀 가져오기:
public static IEnumerable<DependencyObject> FindChildren(this DependencyObject obj)
{
return Enumerable.Range(0, VisualTreeHelper.GetChildrenCount(obj))
.Select(i => VisualTreeHelper.GetChild(obj, i));
}
히아라키 나무 아래 모든 아이들 찾기:
public static IEnumerable<DependencyObject> FindAllChildren(this DependencyObject obj)
{
return obj.FindChildren().SelectAllRecursively(o => o.FindChildren());
}
창에서 호출하여 모든 컨트롤을 가져올 수 있습니다.
수집이 완료되면 LINQ(예: OfType, Where)를 사용할 수 있습니다.
질문이 매우 일반적이기 때문에 매우 사소한 경우에 대한 해답을 찾는 사람들을 끌어들일 수 있습니다. 후손이 아닌 아이만 원한다면 Linq를 사용할 수 있습니다.
private void ItemsControlItem_Loaded(object sender, RoutedEventArgs e)
{
if (SomeCondition())
{
var children = (sender as Panel).Children;
var child = (from Control child in children
where child.Name == "NameTextBox"
select child).First();
child.Focus();
}
}
물론 Children에 대한 루프 반복은 당연합니다.
이러한 옵션은 이미 C#의 Visual Tree를 통과하는 방법에 대해 설명합니다.RelativeSource 마크업 확장을 사용하여 xaml의 비주얼 트리를 통과할 수 있습니다.msdn
유형별로 찾다
Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type <TypeToFind>}}}"
유연한 술어를 사용하는 솔루션은 다음과 같습니다.
public static DependencyObject FindChild(DependencyObject parent, Func<DependencyObject, bool> predicate)
{
if (parent == null) return null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (predicate(child))
{
return child;
}
else
{
var foundChild = FindChild(child, predicate);
if (foundChild != null)
return foundChild;
}
}
return null;
}
예를 들어 다음과 같이 부를 수 있습니다.
var child = FindChild(parent, child =>
{
var textBlock = child as TextBlock;
if (textBlock != null && textBlock.Name == "MyTextBlock")
return true;
else
return false;
}) as TextBlock;
코드에서 특정 유형의 상위 항목을 찾으려면 다음을 사용합니다.
[CanBeNull]
public static T FindAncestor<T>(DependencyObject d) where T : DependencyObject
{
while (true)
{
d = VisualTreeHelper.GetParent(d);
if (d == null)
return null;
var t = d as T;
if (t != null)
return t;
}
}
이 구현에서는 약간 더 빠른 재귀 대신 반복을 사용합니다.
C# 7 을 사용하고 있는 경우는, 조금 짧게 할 수 있습니다.
[CanBeNull]
public static T FindAncestor<T>(DependencyObject d) where T : DependencyObject
{
while (true)
{
d = VisualTreeHelper.GetParent(d);
if (d == null)
return null;
if (d is T t)
return t;
}
}
ControlTemplate가 분리된 XAML 파일에 저장되어 있기 때문에 @CrimsonX 또는 @Drew Noakes 메서드에서 컨트롤을 찾을 수 없습니다.다음 방법으로 컨트롤을 찾았습니다.
private Image backImage;
private void Diagram_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
// here is the example of the ControlTemplate declaration
//<ControlTemplate x:Key="DiagramControlTemplate1" TargetType="{x:Type Diagram}">
var ctrlTemplate = (ControlTemplate)FindResource("DiagramControlTemplate1");
// diagram is the x:Name of TemplatedControl and, "backImage" is the name of control that I want to find.
var imageControl = ctrlTemplate.FindName("backImage", diagram);
if (imageControl != null)
{
this.backImage = (Image)imageControl;
}
}
이건 나한테 효과가 있어.
이 코드는 @CrimsonX answer의 버그를 수정합니다.
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;
}
// 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
{
// child element found.
foundChild = (T)child;
break;
}
}
return foundChild;
}
유형이 일치하지만 이름이 일치하지 않는 경우 메서드를 계속 반복 호출하면 됩니다(합격 시 발생).FrameworkElement
~하듯이T
)를 사용하지 않으면 다시 돌아갑니다.null
그건 틀렸어
제가 자주 사용하는 몇 가지 방법을 소개합니다.
사용방법:
// Starts the search from thisUiElement (might be a UserContol, Window, etc..)
var combobox = thisUiElement.ChildOfType<ComboBox>();
var employeesListBox = thisUiElement.ChildOfName("EmployeesListBox");
// Starts the search from MainWindow to find the first DataGrid
var dataGrid = WpfUtils.ChildOfType<DataGrid>();
// Starts the search from MainWindow to find the all ListViews
List<ComboBox> allListViews = WpfUtils.ChildOfType<ListView>();
// Starts the search from MainWindow to find the element of name EmployeesComboBox
var combobox = WpfUtils.ChildOfName("EmployeesComboBox");
구현:
/*
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
namespace WpfUtilities;
*/
public static class WpfUtils{
public static Window AppMainWindow =>
Application.Current?.MainWindow;
#region Find By Type
// Start the search from MainWindow, example usage: var combobox = WpfUtils.ChildOfType<ComboBox>();
public static T ChildOfType<T>() where T : DependencyObject =>
ChildOfType<T>(AppMainWindow);
/// This will return the first child of type T
public static T ChildOfType<T>(this DependencyObject parent)
where T : DependencyObject
{
if (parent == null) return null;
T child = default;
var numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (var i = 0; i < numVisuals; i++)
{
var v = VisualTreeHelper.GetChild(parent, i);
child = v as T ?? v.ChildOfType<T>();
if (child != null)
break;
}
return child;
}
// Start the search from MainWindow, example usage: List<ComboBox> comboboxes = WpfUtils.ChildOfType<ComboBox>();
public static IEnumerable<T> ChildrenOfType<T>() where T : DependencyObject =>
ChildrenOfType<T>(AppMainWindow);
/// This will not break the search when finding the first kid of type T, but it will keep searching to return all kids of type T
public static IEnumerable<T> ChildrenOfType<T>(
this DependencyObject parent) where T : DependencyObject
{
if (parent == null) yield break;
var numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (var i = 0; i < numVisuals; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is T dependencyObject)
yield return dependencyObject;
foreach (var childOfChild in child.ChildrenOfType<T>())
yield return childOfChild;
}
}
#endregion
#region Find By Name
/// If parent is null, the search will start from MainWindow, example usage: var combobox = WpfUtils.ChildOfName("EmployeesCombobox");
public static FrameworkElement ChildOfName(string childName,
DependencyObject parent = null)
{
parent ??= AppMainWindow;
object child = null;
var numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (var i = 0; i < numVisuals; i++)
{
var v = VisualTreeHelper.GetChild(parent, i);
child = v is FrameworkElement f && f.Name == childName
? f
: ChildOfName(childName, v);
if (child != null)
break;
}
return child as FrameworkElement;
}
#endregion
#region
// Yet another useful method, if you are writing code in a .xaml.cs file and you want to get the parent of a type.. example usage: this.ParentOfType<Grid>(); this.ParentOfType<UserControl>(); this.ParentOfType<Window>();
public static T ParentOfType<T>(this DependencyObject child) where T : DependencyObject
{
var parentDepObj = child;
do
{
parentDepObj = VisualTreeHelper.GetParent(parentDepObj);
if (parentDepObj is T parent) return parent;
} while (parentDepObj != null);
return null;
}
#endregion
}
아래 코드를 사용하여 이름으로 물건을 찾을 수 있었습니다.
stkMultiChildControl = stkMulti.FindChild<StackPanel>("stkMultiControl_" + couter.ToString());
이거 드셔보세요
<TextBlock x:Name="txtblock" FontSize="24" >Hai Welcom to this page
</TextBlock>
코드 이면
var txtblock = sender as Textblock;
txtblock.Foreground = "Red"
언급URL : https://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type
'source' 카테고리의 다른 글
목표 C에서 NSAray를 되돌리려면 어떻게 해야 하나요? (0) | 2023.04.09 |
---|---|
Swift: 객체 배열을 알파벳 순으로 정렬합니다. (0) | 2023.04.09 |
ARC에서 항상 자신의 약한 참조를 블록에 전달하시겠습니까? (0) | 2023.04.09 |
IF 문: 조건이 false일 경우 셀을 공백으로 두는 방법("does not function") (0) | 2023.04.09 |
NSMutable Array를 셔플하는 가장 좋은 방법은 무엇입니까? (0) | 2023.04.09 |