-
[WPF] foreach ControlC#/WPF_예제소스 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>
728x90'C# > WPF_예제소스' 카테고리의 다른 글
[WPF] TextBlock DoubleClick Event (0) 2022.09.22 [WPF] App.config (0) 2022.09.05 [WPF]MVVM Sample (0) 2022.07.11 [WPF] OnPropertyChanged (0) 2022.07.01 [WPF]모눈종이 그리기 (0) 2022.06.29 댓글