Skip to content

Commit 2b42bc7

Browse files
committed
add FindVisualChild
1 parent 0633fde commit 2b42bc7

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/WPFDevelopers.Shared/Core/Helpers/ControlsHelper.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
using System.Windows.Media;
1313
using System.Windows.Media.Animation;
1414
using System.Windows.Media.Imaging;
15-
using WPFDevelopers.Controls;
16-
using WPFDevelopers.Utilities;
1715

1816
namespace WPFDevelopers.Helpers
1917
{
@@ -254,6 +252,21 @@ public static Thickness GetPadding(FrameworkElement element)
254252
return new Thickness();
255253
}
256254

255+
public static T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
256+
{
257+
int childCount = VisualTreeHelper.GetChildrenCount(parent);
258+
for (int i = 0; i < childCount; i++)
259+
{
260+
var child = VisualTreeHelper.GetChild(parent, i);
261+
if (child is T typedChild)
262+
return typedChild;
263+
var result = FindVisualChild<T>(child);
264+
if (result != null)
265+
return result;
266+
}
267+
return null;
268+
}
269+
257270

258271
}
259272

0 commit comments

Comments
 (0)