Skip to content

Instantly share code, notes, and snippets.

@imZack
Last active November 5, 2017 06:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save imZack/5481767 to your computer and use it in GitHub Desktop.
Save imZack/5481767 to your computer and use it in GitHub Desktop.
C# find-all-controls-in-wpf-window-by-type
/// <summary>
/// Form: http://stackoverflow.com/questions/974598/find-all-controls-in-wpf-window-by-type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="depObj"></param>
/// <returns></returns>
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
foreach (CheckBox checkBox in FindVisualChildren<CheckBox>(this))
{
if (checkBox.IsChecked==false) continue;
switch(checkBox.Name)
{
case "handRightBox":
joints.Add(JointType.HandRight);
break;
case "handLeftBox":
joints.Add(JointType.HandLeft);
break;
case "wristRightBox":
joints.Add(JointType.WristRight);
break;
case "wristLeftBox":
joints.Add(JointType.WristLeft);
break;
case "elbowRightBox":
joints.Add(JointType.ElbowRight);
break;
case "elbowLeftBox":
joints.Add(JointType.ElbowLeft);
break;
}
Console.WriteLine("Joint: " + checkBox.Name.Substring(0, checkBox.Name.Length-3) + " added!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment