public static IEnumerable <DependencyObject> WalkLeafFirst_NonRecursive(DependencyObject root, bool logicalInsteadOfVisualTreeScan) { var work = new Stack <KeyValuePair <DependencyObject, bool> >(); work.Push(new KeyValuePair <DependencyObject, bool>(root, false)); while (work.Count > 0) { KeyValuePair <DependencyObject, bool> current = work.Pop(); if (current.Value) { yield return(current.Key); } else { work.Push(new KeyValuePair <DependencyObject, bool>(current.Key, true)); var children = new List <DependencyObject>(); if (logicalInsteadOfVisualTreeScan) { foreach (object c in LogicalTreeHelper.GetChildren(current.Key)) { var child = c as DependencyObject; if (child != null) { children.Add(child); } } } else { for (int i = 0; i != VisualTreeHelper.GetChildrenCount(current.Key); ++i) { DependencyObject child = VisualTreeHelper.GetChild(current.Key, i); children.Add(child); } } for (int i = children.Count - 1; i >= 0; i--) { work.Push(new KeyValuePair <DependencyObject, bool>(children[i], false)); } } } }
public static bool HasErrors(DependencyObject obj) { foreach (object child in LogicalTreeHelper.GetChildren(obj)) { TextBox element = child as TextBox; if (element == null) { continue; } if (Validation.GetHasError(element) || (element.Text.Length == 0)) { return(true); } HasErrors(element); } return(false); }
/// <summary> /// This walks through Logical tree /// and returns ArrayList that contains all of typeof(type) object that found from LogicalTree. /// </summary> /// <param name="o">DependencyObject</param> /// <param name="type">type of element</param> /// <param name="children"></param> /// <returns>ArrayList of typeof(type) object found from Logical Tree.</returns> private static ArrayList GetLogicalChildren(DependencyObject o, Type type, ArrayList children) { if (o != null) { if (o.GetType() == type) { children.Add(o); } IEnumerator enumerator = LogicalTreeHelper.GetChildren(o).GetEnumerator(); while (enumerator.MoveNext()) { DependencyObject current = enumerator.Current as DependencyObject; children = GetLogicalChildren(current, type, children); } } return(children); }
public static T FindObject<T>(this DependencyObject current) where T : DependencyObject { foreach (var item in LogicalTreeHelper.GetChildren(current)) { if (item.GetType() == typeof(T)) return (T)item; if (item is DependencyObject) { var result = FindObject<T>((DependencyObject)item); if (result != null) return result; } } return null; }
private void LoadCheckBoxStates() { Settings.Default.startWithWindows = File.Exists(Program.StartupLink); foreach (var item in LogicalTreeHelper.GetChildren(SettingsGrid)) { switch (item) { case CheckBox chkCast: chkCast.IsChecked = bool.Parse(Settings.Default[chkCast.Name].ToString()); break; case TextBox txtCast: txtCast.Text = Settings.Default[txtCast.Name].ToString(); break; } } }
private static void ShowLogicalControl(DependencyObject tgt, int position = 0) { if (tgt == null) { return; } ShowLog(tgt, position); foreach (var ctl in LogicalTreeHelper.GetChildren(tgt)) { if (ctl is DependencyObject) { int nextPosition = position + 1; ShowLogicalControl((DependencyObject)ctl, nextPosition); } } }
private void speedMenuItem_Click(object sender, RoutedEventArgs e) { System.Windows.Controls.MenuItem clickedMenuItem = (System.Windows.Controls.MenuItem)sender; System.Windows.Controls.MenuItem mainMenuItem = (System.Windows.Controls.MenuItem)(clickedMenuItem).Parent; foreach (var child in LogicalTreeHelper.GetChildren(mainMenuItem)) { if ((child is System.Windows.Controls.MenuItem) && ((System.Windows.Controls.MenuItem)child).IsCheckable) { ((System.Windows.Controls.MenuItem)child).IsChecked = false; } } clickedMenuItem.IsChecked = true; _core.CycleFrequency = Convert.ToInt32(((string)clickedMenuItem.Header).Split(' ')[0].TrimStart('_')); }
//... public static T FindLogicalChildOfType <T>(this DependencyObject input) where T : DependencyObject { if (input is not null) { foreach (var i in LogicalTreeHelper.GetChildren(input)) { if (i is DependencyObject j) { var result = j is T k ? k : FindLogicalChildOfType <T>(j); if (result != null) { return(result); } } } } return(default);
public static void HideBoundingBox(object root) { Control control = root as Control; if (control != null) { control.FocusVisualStyle = null; } if (root is DependencyObject) { foreach (object child in LogicalTreeHelper.GetChildren((DependencyObject)root)) { HideBoundingBox(child); } } }
/// <summary> /// Caches the fields. /// </summary> /// <param name="dependencyObject">The dependency object.</param> /// <param name="fields">The fields dictionary used for caching.</param> private static void CacheLogicalTree(DependencyObject dependencyObject, ICollection <UIElement> fields) { foreach (var child in LogicalTreeHelper.GetChildren(dependencyObject).OfType <DependencyObject>()) { var uiElement = child as UIElement; if (uiElement != null) { var fieldName = NovaValidation.GetFieldName(child); if (!string.IsNullOrEmpty(fieldName)) { fields.Add(uiElement); } } CacheLogicalTree(child, fields); } }
void BuildLogicalTree(int depth, object obj) { //add the type name to the dataToShow member variable _dataToShow += new string(' ', depth) + obj.GetType().Name + "\n"; //If an item is not a DependencyObject, skip it if (!(obj is DependencyObject)) { return; } //Make a recursive call for each logical child foreach (var child in LogicalTreeHelper.GetChildren((DependencyObject)obj)) { BuildLogicalTree(depth + 5, child); } }
private void BuildLogicalTree(DependencyObject current, ItemsControl parentControl) { var newItem = new TreeViewItem { Header = this.GetDebugInfo(current), Tag = current }; parentControl.Items.Add(newItem); var children = LogicalTreeHelper.GetChildren(current); foreach (var child in children.OfType <DependencyObject>()) { this.BuildLogicalTree(child, newItem); } }
/// <summary> /// Gets all FrameworkElement class instance children of a given /// <para/>FrameworkElement class instance recursively, which have an assigned x:Uid. /// </summary> /// <param name="element">A class instance which is or is inherited from a FrameworkElement class.</param> /// <returns>A list of FrameworkElement class instances with an assigned x:Uid.</returns> public static List <FrameworkElement> GetUidElements(FrameworkElement element) { List <FrameworkElement> elements = new List <FrameworkElement>(); foreach (var e in LogicalTreeHelper.GetChildren(element)) { if (e is FrameworkElement el) { if (el.Uid != string.Empty) { elements.Add(el); } elements.AddRange(GetUidElements(el)); } } return(elements); }
public static IEnumerable <DockablePane> DockablePanes(this UIElement element) { if (element is DockablePane) { yield return(element as DockablePane); } foreach (UIElement childObject in LogicalTreeHelper.GetChildren(element)) { if (element is DockablePane) { yield return(element as DockablePane); } yield return(FindChildDockablePane(childObject)); } }
private static IEnumerable <T> GetElementsOfType <T>(DependencyObject parent) where T : DependencyObject { foreach (var c in LogicalTreeHelper.GetChildren(parent)) { if (c is T) { yield return((T)c); } else if (c is DependencyObject) { foreach (var cc in GetElementsOfType <T>((DependencyObject)c)) { yield return(cc); } } } }
static void GetLogicalChildCollection <W>(DependencyObject parent, List <W> logicalCollection) where W : DependencyObject { var children = LogicalTreeHelper.GetChildren(parent); foreach (object child in children) { if (child is DependencyObject) { var dependencyChild = child as DependencyObject; if (child is W) { logicalCollection.Add(child as W); } GetLogicalChildCollection(dependencyChild, logicalCollection); } } }
public static IEnumerable <T> FindLogicalDescendants <T>(this DependencyObject obj) where T : DependencyObject { // Search immediate children first (breadth-first) foreach (DependencyObject childObj in LogicalTreeHelper.GetChildren(obj)) { var child = childObj as T; if (child != null) { yield return(child); } foreach (T descendant in FindLogicalDescendants <T>(childObj)) { yield return(descendant); } } }
private static IEnumerable <T> EnumerateDescendantObjects <T>(DependencyObject obj) where T : DependencyObject { foreach (var child in LogicalTreeHelper.GetChildren(obj)) { if (child is T cobj) { yield return(cobj); } if (child is DependencyObject dobj) { foreach (var cobj2 in EnumerateDescendantObjects <T>(dobj)) { yield return(cobj2); } } } }
static TabControl FindTab(FrameworkElement element) { foreach (var e in LogicalTreeHelper.GetChildren(element).Cast <object>().Where(e => e is FrameworkElement).Cast <FrameworkElement>()) { var tab = e as TabControl; if (tab != null) { return(tab); } tab = FindTab(e); if (tab != null) { return(tab); } } return(null); }
// ReSharper disable once UnusedMember.Local private static void CollectResources( FrameworkElement haveResources , ObservableCollection <ResourceInfo> resourcesCollection ) { var res = haveResources.Resources; AddResourceInfos(res, resourcesCollection); foreach (var child in LogicalTreeHelper.GetChildren(haveResources)) { if (child is FrameworkElement e) { CollectResources(e, resourcesCollection); } } }
protected override void ReloadCore() { base.ReloadCore(); if (this.TreeService.TreeType == TreeType.Visual) { foreach (var child in LogicalTreeHelper.GetChildren(this.PopupTarget)) { if (child is null) { continue; } this.Children.Add(this.TreeService.Construct(child, this)); } } }
internal static List <DependencyObject> LogicalTreeWalk(DependencyObject parent) { List <DependencyObject> elements = new List <DependencyObject>(); IEnumerable children = LogicalTreeHelper.GetChildren(parent); foreach (object child in children) { DependencyObject childTree = child as DependencyObject; if (childTree != null) { elements.Add(childTree); elements.AddRange(LogicalTreeWalk(childTree)); } } return(elements); }
public static IEnumerable <T> GetChildrenOf <T>(this DependencyObject obj) { foreach (object child in LogicalTreeHelper.GetChildren(obj)) { if (child is T) { yield return((T)child); } else if (child is DependencyObject) { foreach (var subChild in GetChildrenOf <T>((DependencyObject)child)) { yield return(subChild); } } } }
private static void GetLogicalChildCollection <T>(DependencyObject parent, List <T> logicalCollection) where T : DependencyObject { IEnumerable children = LogicalTreeHelper.GetChildren(parent); foreach (object child in children) { if (child is DependencyObject) { DependencyObject depChild = child as DependencyObject; if (child is T) { logicalCollection.Add(child as T); } GetLogicalChildCollection(depChild, logicalCollection); } } }
public static IEnumerable GetLogicalDescendants(this DependencyObject self) { foreach (var child in LogicalTreeHelper.GetChildren(self)) { if (child is DependencyObject) { foreach (var item in (child as DependencyObject).GetLogicalTree()) { yield return(item); } } else { yield return(child); } } }
/// <summary> /// Validate all dependency objects in a window /// </summary> /// <param name="node"></param> /// <returns></returns> public static bool IsValid(this DependencyObject node) { // Check if dependency object was passed if (node != null) { // Check if dependency object is valid. // NOTE: Validation.GetHasError works for controls that have validation rules attached bool isValid = !Validation.GetHasError(node); if (!isValid) { // If the object is a child of a TabItem, select that item var tab = node.FindLogicalParent <TabItem>(); if (tab != null) { var tabControl = tab.FindLogicalParent <TabControl>(); tabControl.SelectedItem = tab; } // If the dependency object is invalid, and it can receive the focus, // set the focus if (node is IInputElement) { Keyboard.Focus((IInputElement)node); } return(false); } } // If this dependency object is valid, check all child dependency objects foreach (object subnode in LogicalTreeHelper.GetChildren(node)) { if (subnode is DependencyObject) { // If a child dependency object is invalid, return false immediately, // otherwise keep checking if (IsValid((DependencyObject)subnode) == false) { return(false); } } } // All dependency objects are valid return(true); }
private void TranslateElement(FrameworkElement element, BamlControl baml) { if ((element == null) || (baml == null)) { return; } // Translate child controls IEnumerable children = LogicalTreeHelper.GetChildren(element); foreach (object child in children) { if (child is FrameworkElement) { FrameworkElement childElement = (FrameworkElement)child; TranslateElement(childElement, baml.Find(childElement)); } } // Translate this control if ((TranslateElementEvent == null) || TranslateElementEvent(this, element)) { PropertyInfo[] properties = element.GetType().GetProperties(); foreach (PropertyInfo property in properties) { Object value; if (property.Name == "Content") { value = baml.Value; } else { value = baml.FindProperty(property.Name); } if ((value != null) && property.CanWrite && (value is string)) { //property.SetValue(element, ((string)value).ToUpper(), null); property.SetValue(element, value, null); } } } }
public static bool IsInRootPopup(DependencyObject element) { if (IsMousePhysicallyOver(element as UIElement)) { return(true); } // Check if is drop down control var dropDown = element as DropDownCommandButton; if (dropDown != null && dropDown.IsDropDownOpen && dropDown.Popup != null && dropDown.Popup.Child != null && IsMousePhysicallyOver(dropDown.Popup.Child)) { return(true); } // Check if is context menu var menu = element as ContextMenu; if (menu != null && menu.IsOpen && IsMousePhysicallyOver(menu)) { return(true); } // Check if is menu item var menuItem = element as MenuItem; if (menuItem != null && menuItem.IsSubmenuOpen && IsMousePhysicallyOver(menuItem.SubmenuPopup().Child)) { return(true); } // Check if is Popup var popup = element as Popup; if (popup != null && popup.IsOpen && IsMousePhysicallyOver(popup.Child)) { return(true); } // Check childs var children = LogicalTreeHelper.GetChildren(element); // we are only interested in children of type FrameworkElement return(children.OfType <FrameworkElement>().Any(child => child.IsVisible && IsInRootPopup(child))); }
/// <summary> /// ValidateInput() - Validate the request input and populate the error response /// </summary> /// <returns>Error response, blank is no errors.</returns> private string ValidateInput() { List <string> errorMsg = new List <string>(); StringBuilder sb = new StringBuilder(); bool gotTemplate = false; if (txtConnStr.Text == "") { errorMsg.Add("Please enter a Connection String"); } if (txtSaveLocation.Text == "") { errorMsg.Add("Please select a Save Location"); } if (ColDataList.Count == 0) { errorMsg.Add("Please Pick a Table"); } IdColumnCheck(errorMsg); ValidateActiveInactiveInfo(errorMsg); var children = LogicalTreeHelper.GetChildren(panTemplates); foreach (var item in children) { var workItem = item as CheckBox; if (workItem.IsChecked == true) { gotTemplate = true; break; } } if (!gotTemplate) { errorMsg.Add("Please select at least 1 Template"); } if (errorMsg.Count > 0) { sb.AppendLine("Validation errors occurred:"); errorMsg.ForEach(em => sb.AppendLine("- " + em)); } return(sb.ToString()); }
public static IEnumerable <string> FindAllNames(this DependencyObject element) { //var element = (T)rootElement.FindName(name); //if (element != null) return element; //LogicalTreeHelper.FindLogicalNode(element, name); var type = element.GetType(); var svgid = SharpVectors.Runtime.SvgObject.GetId(element); if (svgid != null) { yield return(svgid); } var names = type.GetCustomAttributes(true) .OfType <System.Windows.Markup.RuntimeNamePropertyAttribute>() .Select(a => type.GetProperty(a.Name)) .Where(p => p != null) .Select(p => p.GetValue(element, new object[0]) as string) .Where(n => !string.IsNullOrEmpty(n)); if (element is Image) { names = names.Union(((Image)element).Source.FindAllNames()); } else if (element is DrawingImage) { names = names.Union(((DrawingImage)element).Drawing.FindAllNames()); } else if (element is DrawingGroup) { names = names.Union(((DrawingGroup)element).Children .SelectMany(child => child.FindAllNames())); } names = names.Union(LogicalTreeHelper.GetChildren(element) .OfType <DependencyObject>() .SelectMany(child => child.FindAllNames())); foreach (var name in names) { yield return(name); } }