Как я могу найти элементы управления WPF по имени или типу?
Вопрос
Мне нужно выполнить поиск в иерархии элементов управления WPF для элементов управления, которые соответствуют заданному имени или типу.Как я могу это сделать?
Решение
Я скомбинировал формат шаблона, использованный Джоном Мычеком и алгоритмом 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.Найденное имя(строка).
Данный:
<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);
}
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 также обходит логические поддеревья.Смотрите пост Джоша Смита, ссылка на который приведена в сообщении в блоге.
Источник: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
Если вы хотите найти ВСЕ элементы управления определенного типа, вас может заинтересовать и этот фрагмент
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;
}
}
}
Я редактировал код 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;
}
Это исключит некоторые элементы - вы должны расширить его таким образом, чтобы поддерживать более широкий набор элементов управления. Для краткого обсуждения посмотрите здесь
/// <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);
}
}
Хотя я люблю рекурсию в целом, она не так эффективна, как итерация при программировании на C #, так что, возможно, следующее решение лучше предложенного Джоном Мычеком? При этом выполняется поиск иерархии из данного элемента управления, чтобы найти элемент управления предка определенного типа.
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;
}
Назовите его так, чтобы найти Окно
, содержащее элемент управления с именем 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();
}
}
exciton80 ... У меня возникла проблема с тем, что ваш код не рекурсировал через пользовательский контроль. Он попадал в корень сетки и выдавал ошибку. Я считаю, что это исправляет это для меня:
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 #. Можно также просмотреть визуальное дерево в xaml, используя расширение разметки RelativeSource. 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;
Этот код просто исправляет ошибку в ответе @CrimsonX:
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
, и это неправильно. Р>
Чтобы найти предка данного типа из кода, вы можете использовать:
[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;
}
}
Попробуйте это
<TextBlock x:Name="txtblock" FontSize="24" >Hai Welcom to this page
</TextBlock>
Код позади
var txtblock = sender as Textblock;
txtblock.Foreground = "Red"