Skip to content

Commit 81a5ca4

Browse files
committed
Added FindChildOrSelf overloads
1 parent 73ab43e commit 81a5ca4

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

Microsoft.Toolkit.Uwp.UI/Extensions/Tree/LogicalTree.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,94 @@ public static class LogicalTree
193193
return null;
194194
}
195195

196+
/// <summary>
197+
/// Find the first child (or self) of type <see cref="FrameworkElement"/> with a given name, using a depth-first search.
198+
/// </summary>
199+
/// <param name="element">The root element.</param>
200+
/// <param name="name">The name of the element to look for.</param>
201+
/// <param name="comparisonType">The comparison type to use to match <paramref name="name"/>.</param>
202+
/// <returns>The child (or self) that was found, or <see langword="null"/>.</returns>
203+
public static FrameworkElement? FindChildOrSelf(this FrameworkElement element, string name, StringComparison comparisonType = StringComparison.Ordinal)
204+
{
205+
if (name.Equals(element.Name, comparisonType))
206+
{
207+
return element;
208+
}
209+
210+
return FindChild(element, name, comparisonType);
211+
}
212+
213+
/// <summary>
214+
/// Find the first child (or self) element of a given type, using a depth-first search.
215+
/// </summary>
216+
/// <typeparam name="T">The type of elements to match.</typeparam>
217+
/// <param name="element">The root element.</param>
218+
/// <returns>The child (or self) that was found, or <see langword="null"/>.</returns>
219+
public static T? FindChildOrSelf<T>(this FrameworkElement element)
220+
where T : notnull, FrameworkElement
221+
{
222+
if (element is T result)
223+
{
224+
return result;
225+
}
226+
227+
return FindChild<T>(element);
228+
}
229+
230+
/// <summary>
231+
/// Find the first child (or self) element of a given type, using a depth-first search.
232+
/// </summary>
233+
/// <param name="element">The root element.</param>
234+
/// <param name="type">The type of element to match.</param>
235+
/// <returns>The child (or self) that was found, or <see langword="null"/>.</returns>
236+
public static FrameworkElement? FindChildOrSelf(this FrameworkElement element, Type type)
237+
{
238+
if (element.GetType() == type)
239+
{
240+
return element;
241+
}
242+
243+
return FindChild(element, type);
244+
}
245+
246+
/// <summary>
247+
/// Find the first child (or self) element matching a given predicate, using a depth-first search.
248+
/// </summary>
249+
/// <typeparam name="T">The type of elements to match.</typeparam>
250+
/// <param name="element">The root element.</param>
251+
/// <param name="predicate">The predicatee to use to match the child nodes.</param>
252+
/// <returns>The child (or self) that was found, or <see langword="null"/>.</returns>
253+
public static T? FindChildOrSelf<T>(this FrameworkElement element, Func<T, bool> predicate)
254+
where T : notnull, FrameworkElement
255+
{
256+
if (element is T result && predicate(result))
257+
{
258+
return result;
259+
}
260+
261+
return FindChild(element, predicate);
262+
}
263+
264+
/// <summary>
265+
/// Find the first child (or self) element matching a given predicate, using a depth-first search.
266+
/// </summary>
267+
/// <typeparam name="T">The type of elements to match.</typeparam>
268+
/// <typeparam name="TState">The type of state to use when matching nodes.</typeparam>
269+
/// <param name="element">The root element.</param>
270+
/// <param name="state">The state to give as input to <paramref name="predicate"/>.</param>
271+
/// <param name="predicate">The predicatee to use to match the child nodes.</param>
272+
/// <returns>The child (or self) that was found, or <see langword="null"/>.</returns>
273+
public static T? FindChildOrSelf<T, TState>(this FrameworkElement element, TState state, Func<T, TState, bool> predicate)
274+
where T : notnull, FrameworkElement
275+
{
276+
if (element is T result && predicate(result, state))
277+
{
278+
return result;
279+
}
280+
281+
return FindChild(element, state, predicate);
282+
}
283+
196284
/// <summary>
197285
/// Find all logical child controls of the specified type.
198286
/// </summary>

0 commit comments

Comments
 (0)