C#/WPF_예제소스

[WPF] foreach Control

딸기우유중독 2022. 8. 17. 17:47
public MainPage()
        {
            InitializeComponent();
            this.Loaded += MainPage_Loaded;

        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (Button button in FindVisualChildren<Button>(mainGrid))
            {
                button.Background = new SolidColorBrush(Colors.Black);
            }
        }

        private 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;
                    }
                }
            }
        }
<Page ...>
<Grid x:Name="mainGrid">
...
</Grid>
</Page>

 

 

https://social.msdn.microsoft.com/Forums/vstudio/en-US/b2a9baba-8894-4f53-a488-a6417f858083/reach-all-buttons-of-xaml-by-c?forum=wpf 

 

reach all buttons of xaml by c#

Hi Giga Bokuchava, This forum is discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.  Based on your description, I suspect that you are developing WPF application. Am I right? If yes, I will help you move this

social.msdn.microsoft.com

 

728x90