Tell me, how can I get all the buttons from the wpf form and drive them into an array?

  • What for do you need it? You should not need it. - VladD

1 answer 1

using System.Collections.Generic; using System.Windows; using System.Windows.Media; ... public static List<T> GetVisualChilds<T>(DependencyObject parent) where T : DependencyObject { List<T> childs = new List<T>(); int numVisuals = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < numVisuals; i++) { DependencyObject v = VisualTreeHelper.GetChild(parent, i); if (v is T) childs.Add(v as T); childs.AddRange(GetVisualChilds<T>(v)); } return childs; } ... //Естественно this - это Ваша форма или контрол для поиска List<Button> allButtons = GetVisualChilds<Button>(this.Content as DependencyObject); 
  • Screwed ... For some reason in the list of 0 items. The form passed ... - FastTI
  • Buttons added as <button/> ? Do you exactly execute the code after adding buttons to the form? - Dmitry Chistik
  • <Button x: Name = "Add_SettingsPC" Content = "Add" HorizontalAlignment = "Left" Margin = "147,67,0,0" VerticalAlignment = "Top" Width = "75" Click = "Add_SettingsPC_Click" /> I execute code after Download Form - FastTI
  • Yes, I agree, you need GetVisualChilds<Button>(this.Content as DependencyObject) - Dmitry Chistik