Last active
November 5, 2017 06:58
-
-
Save imZack/5481767 to your computer and use it in GitHub Desktop.
C# find-all-controls-in-wpf-window-by-type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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