Skip to content

Commit 6fe63a6

Browse files
committed
Added optimized content paths for logical tree extensions
1 parent dce4311 commit 6fe63a6

File tree

1 file changed

+117
-9
lines changed

1 file changed

+117
-9
lines changed

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

Lines changed: 117 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,50 @@ public static class LogicalTree
110110
}
111111
}
112112
}
113-
else if (element.GetContentControl() is FrameworkElement contentControl)
113+
else if (element is UserControl userControl)
114114
{
115-
if (contentControl is T result && predicate(result))
115+
if (userControl.Content is T result && predicate(result))
116116
{
117117
return result;
118118
}
119119

120-
return FindChild(contentControl, predicate);
120+
if (userControl.Content is FrameworkElement content)
121+
{
122+
return FindChild(content, predicate);
123+
}
124+
}
125+
else if (element is ContentControl contentControl)
126+
{
127+
if (contentControl.Content is T result && predicate(result))
128+
{
129+
return result;
130+
}
131+
132+
if (contentControl.Content is FrameworkElement content)
133+
{
134+
return FindChild(content, predicate);
135+
}
136+
}
137+
else if (element is Border border)
138+
{
139+
if (border.Child is T result && predicate(result))
140+
{
141+
return result;
142+
}
143+
144+
if (border.Child is FrameworkElement child)
145+
{
146+
return FindChild(child, predicate);
147+
}
148+
}
149+
else if (element.GetContentControl() is FrameworkElement containedControl)
150+
{
151+
if (containedControl is T result && predicate(result))
152+
{
153+
return result;
154+
}
155+
156+
return FindChild(containedControl, predicate);
121157
}
122158

123159
return null;
@@ -179,14 +215,50 @@ public static class LogicalTree
179215
}
180216
}
181217
}
182-
else if (element.GetContentControl() is FrameworkElement contentControl)
218+
else if (element is UserControl userControl)
219+
{
220+
if (userControl.Content is T result && predicate(result, state))
221+
{
222+
return result;
223+
}
224+
225+
if (userControl.Content is FrameworkElement content)
226+
{
227+
return FindChild(content, state, predicate);
228+
}
229+
}
230+
else if (element is ContentControl contentControl)
231+
{
232+
if (contentControl.Content is T result && predicate(result, state))
233+
{
234+
return result;
235+
}
236+
237+
if (contentControl.Content is FrameworkElement content)
238+
{
239+
return FindChild(content, state, predicate);
240+
}
241+
}
242+
else if (element is Border border)
243+
{
244+
if (border.Child is T result && predicate(result, state))
245+
{
246+
return result;
247+
}
248+
249+
if (border.Child is FrameworkElement child)
250+
{
251+
return FindChild(child, state, predicate);
252+
}
253+
}
254+
else if (element.GetContentControl() is FrameworkElement containedControl)
183255
{
184-
if (contentControl is T result && predicate(result, state))
256+
if (containedControl is T result && predicate(result, state))
185257
{
186258
return result;
187259
}
188260

189-
return FindChild(contentControl, state, predicate);
261+
return FindChild(containedControl, state, predicate);
190262
}
191263

192264
return null;
@@ -328,11 +400,47 @@ public static IEnumerable<FrameworkElement> FindChildren(this FrameworkElement e
328400
}
329401
}
330402
}
331-
else if (element.GetContentControl() is FrameworkElement contentControl)
403+
else if (element is UserControl userControl)
404+
{
405+
if (userControl.Content is FrameworkElement content)
406+
{
407+
yield return content;
408+
409+
foreach (FrameworkElement childOfContent in FindChildren(content))
410+
{
411+
yield return childOfContent;
412+
}
413+
}
414+
}
415+
else if (element is ContentControl contentControl)
416+
{
417+
if (contentControl.Content is FrameworkElement content)
418+
{
419+
yield return content;
420+
421+
foreach (FrameworkElement childOfContent in FindChildren(content))
422+
{
423+
yield return childOfContent;
424+
}
425+
}
426+
}
427+
else if (element is Border border)
428+
{
429+
if (border.Child is FrameworkElement child)
430+
{
431+
yield return child;
432+
433+
foreach (FrameworkElement childOfChild in FindChildren(child))
434+
{
435+
yield return childOfChild;
436+
}
437+
}
438+
}
439+
else if (element.GetContentControl() is FrameworkElement containedControl)
332440
{
333-
yield return contentControl;
441+
yield return containedControl;
334442

335-
foreach (FrameworkElement childOfChild in FindChildren(contentControl))
443+
foreach (FrameworkElement childOfChild in FindChildren(containedControl))
336444
{
337445
yield return childOfChild;
338446
}

0 commit comments

Comments
 (0)