FrameworkElement obj = ControlHelper.SearchControls(Application.Current.MainWindow, "__watermark__"); if (obj != null) { (obj as Grid).Children.Clear(); } public static FrameworkElement SearchControls(DependencyObject control,string tagKey) { FrameworkElement res = null; // 遍历当前控件的所有逻辑子控件 for (int i = 0; i < VisualTreeHelper.GetChildrenCount(control); i++) { DependencyObject child = VisualTreeHelper.GetChild(control, i); if ((child as FrameworkElement).Tag != null) { if ((child as FrameworkElement).Tag.ToString().ToLower() == tagKey.ToLower()) { res = child as FrameworkElement; break; } } res = SearchControls(child, tagKey); if (res != null) { break; } } return res; }
标签:control,tagKey,res,移除,水印,FrameworkElement,child,null From: https://www.cnblogs.com/coolyylu/p/18311436