/// <summary> /// Finds all parents of a control, until the parent with a specified name is found. /// </summary> /// <param name="control">Dependency object whose parents are found.</param> /// <param name="parentName">The name of the parent control where the traversal stops.</param> /// <returns>The ControlSet object containing all the parents found.</returns> public static ControlSet ParentsUpto(DependencyObject control, string parentName) { ControlSet allParents = new ControlSet(); while (true) { DependencyObject parent = VisualTreeHelper.GetParent(control); if (parent == null) { break; } else if (parent is FrameworkElement && ((FrameworkElement)parent).Name.Equals(parentName)) { allParents.Add(parent); break; } else { allParents.Add(parent); control = parent; } } return(allParents); }
/// <summary> /// Finds all parents of a control, until a parent of specified type is found. /// </summary> /// <typeparam name="T">The type of parent control where the traversal stops.</typeparam> /// <param name="control">Dependency object whose parents are found.</param> /// <returns>The ControlSet object containing all the parents found.</returns> public static ControlSet ParentsUpto <T>(DependencyObject control) { ControlSet allParents = new ControlSet(); while (true) { DependencyObject parent = VisualTreeHelper.GetParent(control); if (parent == null) { break; } else if (parent is T) { allParents.Add(parent); break; } else { allParents.Add(parent); control = parent; } } return(allParents); }
public ControlSet Execute(ControlSet superSet) { Common.AddToLog("executing filter-q: " + this.Query); if (Tokens.Count < 2) { return(new ControlSet()); //filter-selector should have minimum two tokens, the first one being the filter-delimiter symbol } ControlSet matchedControls = new ControlSet(); if (Type == FilterSelectorType.Style) { string styleName = Tokens[1].Content; Style styleObject = ControlUtility.GetStyleByName(styleName); if (styleObject != null) { foreach (DependencyObject control in superSet) //find controls by style { if (control is FrameworkElement) { FrameworkElement element = (FrameworkElement)control; if (element.Style != null) { //if (element.Style.Equals(styleObject)) if (ControlUtility.IsStyleEqualsOrBasedOn(element.Style, styleObject)) { Common.AddToLog("matched-by-style: " + element.Name + " based on style"); matchedControls.Add(control); } } } } } } else if (Type == FilterSelectorType.Name) { string name = Tokens[1].Content; foreach (DependencyObject control in superSet) //find control by name { if (control is FrameworkElement) { if (((FrameworkElement)control).Name.Equals(name)) { Common.AddToLog("matched-by-name: " + ((FrameworkElement)control).Name); matchedControls.Add(control); } } } } else if (Type == FilterSelectorType.Property) { matchedControls.AddRange(FilterByPropertyQuery(superSet)); } return(matchedControls); }
private ControlSet FilterByDependencyProperty(ControlSet superSet, FilterType filterType, int propertyPartsLength, string propertyNameDescriptor, string propertyRawValue) { Common.AddToLog("checking for dependency-property: " + propertyNameDescriptor); FieldInfo dependencyPropertyField = ControlUtility.ResolveDependencyProperty(propertyNameDescriptor); if (dependencyPropertyField != null) //if it is a dependency property { ControlSet matchedControls = new ControlSet(); foreach (DependencyObject control in superSet) { object dependencyPropertyObject = dependencyPropertyField.GetValue(control); if (dependencyPropertyObject is DependencyProperty) { DependencyProperty dependencyProperty = (DependencyProperty)dependencyPropertyObject; object dependencyPropertyValue = control.GetValue(dependencyProperty); if (propertyPartsLength == 1) { object defaultValue = dependencyProperty.GetType().IsValueType ? Activator.CreateInstance(dependencyProperty.GetType()) : null; if (dependencyPropertyValue != defaultValue) { Common.AddToLog("matched by dependency-property-name: " + control.ToString()); matchedControls.Add(control); } } else if (propertyPartsLength == 2) { if (ControlSet.CheckPropertyValue(dependencyPropertyValue, propertyRawValue, filterType)) { Common.AddToLog("matched by dependency-property-value: " + control.ToString()); matchedControls.Add(control); } } } } return(matchedControls); } else { Common.AddToLog("dependency-property-null: " + propertyNameDescriptor); } return(null); }
private ControlSet FilterByNormalProperty(ControlSet superSet, FilterType filterType, int propertyPartsLength, string propertyName, string propertyRawValue) { Common.AddToLog("checking for property: " + propertyName); ControlSet matchedControls = new ControlSet(); foreach (DependencyObject control in superSet) { PropertyInfo propertyInfo = control.GetType().GetProperty(propertyName); if (propertyInfo != null) { object propertyValue = propertyInfo.GetValue(control, null); if (propertyPartsLength == 1) { object defaultValue = propertyInfo.PropertyType.IsValueType ? Activator.CreateInstance(propertyInfo.PropertyType) : null; if (propertyValue != defaultValue) { Common.AddToLog("matched-by-property-name: " + control.ToString()); matchedControls.Add(control); } } else if (propertyPartsLength == 2) { if (ControlSet.CheckPropertyValue(propertyValue, propertyRawValue, filterType)) { Common.AddToLog("matched-by-property-value: " + control.ToString()); matchedControls.Add(control); } else { Common.AddToLog("not-matched: " + propertyValue + ", " + propertyRawValue); } } } else { Common.AddToLog("property-null: " + propertyName); } } return(matchedControls); }
public ControlSet Execute(ControlSet superSet) { Common.AddToLog("executing simple-query: " + this.Query); if (Type == SimpleSelectorType.None) { return(new ControlSet()); } //set main controls ControlSet mainControls = new ControlSet(); if (Type == SimpleSelectorType.Universal) { mainControls.AddRange(superSet); } else if (Type == SimpleSelectorType.Element) { Type givenControlType = ControlUtility.ResolveControlType(MainToken.Content); if (givenControlType != null) { foreach (DependencyObject control in superSet) { //if (givenControlType.IsAssignableFrom(control.GetType())) //if (givenControlType.IsSubclassOf(control.GetType())) //if (givenControlType.FullName.Equals(control.GetType().FullName)) if (givenControlType.IsInstanceOfType(control)) { mainControls.Add(control); } } } } //apply filter-selectors if (this.FilterSelectors.Count == 0) { return(mainControls); } else { foreach (FilterSelector filterSelector in this.FilterSelectors) { mainControls = filterSelector.Execute(mainControls); } return(mainControls); } }
public ControlSet Execute(ControlSet superSet) { Common.AddToLog("executing filter-q: " + this.Query); if (Tokens.Count < 2) return (new ControlSet()); //filter-selector should have minimum two tokens, the first one being the filter-delimiter symbol ControlSet matchedControls = new ControlSet(); if (Type == FilterSelectorType.Style) { string styleName = Tokens[1].Content; Style styleObject = ControlUtility.GetStyleByName(styleName); if (styleObject != null) { foreach (DependencyObject control in superSet) //find controls by style { if (control is FrameworkElement) { FrameworkElement element = (FrameworkElement)control; if (element.Style != null) { //if (element.Style.Equals(styleObject)) if (ControlUtility.IsStyleEqualsOrBasedOn(element.Style, styleObject)) { Common.AddToLog("matched-by-style: " + element.Name + " based on style"); matchedControls.Add(control); } } } } } } else if (Type == FilterSelectorType.Name) { string name = Tokens[1].Content; foreach (DependencyObject control in superSet) //find control by name { if (control is FrameworkElement) { if (((FrameworkElement)control).Name.Equals(name)) { Common.AddToLog("matched-by-name: " + ((FrameworkElement)control).Name); matchedControls.Add(control); } } } } else if (Type == FilterSelectorType.Property) { matchedControls.AddRange(FilterByPropertyQuery(superSet)); } return (matchedControls); }
private ControlSet FilterByNormalProperty(ControlSet superSet, FilterType filterType, int propertyPartsLength, string propertyName, string propertyRawValue) { Common.AddToLog("checking for property: " + propertyName); ControlSet matchedControls = new ControlSet(); foreach (DependencyObject control in superSet) { PropertyInfo propertyInfo = control.GetType().GetProperty(propertyName); if (propertyInfo != null) { object propertyValue = propertyInfo.GetValue(control, null); if (propertyPartsLength == 1) { object defaultValue = propertyInfo.PropertyType.IsValueType ? Activator.CreateInstance(propertyInfo.PropertyType) : null; if (propertyValue != defaultValue) { Common.AddToLog("matched-by-property-name: " + control.ToString()); matchedControls.Add(control); } } else if (propertyPartsLength == 2) { if (ControlSet.CheckPropertyValue(propertyValue, propertyRawValue, filterType)) { Common.AddToLog("matched-by-property-value: " + control.ToString()); matchedControls.Add(control); } else { Common.AddToLog("not-matched: " + propertyValue + ", " + propertyRawValue); } } } else { Common.AddToLog("property-null: " + propertyName); } } return (matchedControls); }
private ControlSet FilterByDependencyProperty(ControlSet superSet, FilterType filterType, int propertyPartsLength, string propertyNameDescriptor, string propertyRawValue) { Common.AddToLog("checking for dependency-property: " + propertyNameDescriptor); FieldInfo dependencyPropertyField = ControlUtility.ResolveDependencyProperty(propertyNameDescriptor); if (dependencyPropertyField != null) //if it is a dependency property { ControlSet matchedControls = new ControlSet(); foreach (DependencyObject control in superSet) { object dependencyPropertyObject = dependencyPropertyField.GetValue(control); if (dependencyPropertyObject is DependencyProperty) { DependencyProperty dependencyProperty = (DependencyProperty)dependencyPropertyObject; object dependencyPropertyValue = control.GetValue(dependencyProperty); if (propertyPartsLength == 1) { object defaultValue = dependencyProperty.GetType().IsValueType ? Activator.CreateInstance(dependencyProperty.GetType()) : null; if (dependencyPropertyValue != defaultValue) { Common.AddToLog("matched by dependency-property-name: " + control.ToString()); matchedControls.Add(control); } } else if (propertyPartsLength == 2) { if (ControlSet.CheckPropertyValue(dependencyPropertyValue, propertyRawValue, filterType)) { Common.AddToLog("matched by dependency-property-value: " + control.ToString()); matchedControls.Add(control); } } } } return (matchedControls); } else { Common.AddToLog("dependency-property-null: " + propertyNameDescriptor); } return (null); }
public ControlSet Execute(ControlSet superSet) { Common.AddToLog("executing combinator: " + this.Type); ControlSet result = new ControlSet(); switch (this.Type) { case CombinatorType.Descendant: //get descendants (children in visual-tree) of all controls in superset foreach (DependencyObject control in superSet) { ControlSet controlSet = XamlQuery.All(control); foreach (DependencyObject c in controlSet) { if (!result.Contains(c)) { result.Add(c); } } } break; case CombinatorType.Child: //get children of all Panel controls in superset foreach (DependencyObject control in superSet) { if (control is Panel) { result.AddRange(from child in ((Panel)control).Children select(DependencyObject) child); } } break; case CombinatorType.Adjacent: //get children of all parent Panels of all controls in superset //if a parent is not a Panel, then it is ignored foreach (DependencyObject control in superSet) { if (control is FrameworkElement) { DependencyObject parent = ((FrameworkElement)control).Parent; if (parent is Panel) { result.AddRange(from child in ((Panel)parent).Children select(DependencyObject) child); } } } //include original controls also in result result.AddRange(superSet); Common.AddToLog("adj-superset-added"); break; } //remove duplicate-controls ControlSet uniqueControls = new ControlSet(); foreach (DependencyObject c in result) { if (!uniqueControls.Contains(c)) { uniqueControls.Add(c); } } return(uniqueControls); }
public ControlSet Execute(ControlSet superSet) { Common.AddToLog("executing simple-query: " + this.Query); if (Type == SimpleSelectorType.None) return (new ControlSet()); //set main controls ControlSet mainControls = new ControlSet(); if (Type == SimpleSelectorType.Universal) { mainControls.AddRange(superSet); } else if (Type == SimpleSelectorType.Element) { Type givenControlType = ControlUtility.ResolveControlType(MainToken.Content); if (givenControlType != null) { foreach (DependencyObject control in superSet) { //if (givenControlType.IsAssignableFrom(control.GetType())) //if (givenControlType.IsSubclassOf(control.GetType())) //if (givenControlType.FullName.Equals(control.GetType().FullName)) if (givenControlType.IsInstanceOfType(control)) { mainControls.Add(control); } } } } //apply filter-selectors if (this.FilterSelectors.Count == 0) { return (mainControls); } else { foreach (FilterSelector filterSelector in this.FilterSelectors) { mainControls = filterSelector.Execute(mainControls); } return (mainControls); } }
public ControlSet Execute(ControlSet superSet) { Common.AddToLog("executing combinator: " + this.Type); ControlSet result = new ControlSet(); switch (this.Type) { case CombinatorType.Descendant: //get descendants (children in visual-tree) of all controls in superset foreach (DependencyObject control in superSet) { ControlSet controlSet = XamlQuery.All(control); foreach (DependencyObject c in controlSet) { if (!result.Contains(c)) { result.Add(c); } } } break; case CombinatorType.Child: //get children of all Panel controls in superset foreach (DependencyObject control in superSet) { if (control is Panel) { foreach (var child in ((Panel)control).Children) { result.Add((DependencyObject)child); } } } break; case CombinatorType.Adjacent: //get children of all parent Panels of all controls in superset //if a parent is not a Panel, then it is ignored foreach (DependencyObject control in superSet) { if (control is FrameworkElement) { DependencyObject parent = ((FrameworkElement)control).Parent; if (parent is Panel) { foreach (var child in ((Panel)control).Children) { result.Add((DependencyObject)child); } } } } //include original controls also in result result.AddRange(superSet); Common.AddToLog("adj-superset-added"); break; } //remove duplicate-controls ControlSet uniqueControls = new ControlSet(); foreach (DependencyObject c in result) { if (!uniqueControls.Contains(c)) { uniqueControls.Add(c); } } return (uniqueControls); }