/// <summary> /// Searchs an element in the visual tree matching with the given hash code. /// </summary> /// <param name="obj">A element in the visual tree as root element to search downwards the tree.</param> /// <param name="hashCode">The hash code to search for.</param> /// <returns>The element in the visual three matching the given hash code, otherwise null</returns> /// <remarks> /// This method is especially used in case where the visual tree is not part of the /// main visual tree of the Silverlight application, as it happen with popup controls. /// Optain the hash code from the output of WriteDownwards/WriteUpwards. /// </remarks> public static DependencyObject GetElementByHashCode(DependencyObject obj, int hashCode) { if (obj == null) return null; // check if hash code matches if (obj.GetHashCode() == hashCode) return obj; // store child count local int childCount = VisualTreeHelper.GetChildrenCount(obj); // check if one of the child match to the hash code for (int i = 0; i < childCount; i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child.GetHashCode() == hashCode) return child; } // go down the tree for (int i = 0; i < childCount; i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); DependencyObject returnObj = GetElementByHashCode(child, hashCode); // if object was found, return if (returnObj != null) return returnObj; } // nothing found return null; }
// We don't expect to need this function. [Required when overriding Equals()] // Write a good HashCode anyway (if not a fast one) override public int GetHashCode() { int hashCode; Expression ex = (Expression)_wrEX.Target; hashCode = (null == ex) ? 0 : ex.GetHashCode(); if (null != _wrDO) { DependencyObject DO = (DependencyObject)_wrDO.Target; hashCode += (null == DO) ? 0 : DO.GetHashCode(); } hashCode += (null == _DP) ? 0 : _DP.GetHashCode(); return(hashCode); }
/// <summary> /// Initializes a new instance of the <see cref="ObjectValue"/> class. /// </summary> /// <param name="targetObject">The owner of the property.</param> /// <exception cref="ArgumentNullException"><paramref name="obj"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException"><paramref name="property"/> is <c>null</c>.</exception> protected LocalizedProperty(DependencyObject obj, object property) { if (obj == null) { throw new ArgumentNullException("obj"); } if (property == null) { throw new ArgumentNullException("property"); } _object = new WeakReference(obj); Property = property; _hashCode = obj.GetHashCode() ^ property.GetHashCode(); }
/// <summary> /// Walks up the tree from the given object and collects tree information. /// </summary> private static void CollectTreeUp(DependencyObject obj, List<string> levels) { if (obj == null) return; // name of type string node = GetTypeName(obj); // XAML name node += " " + GetElementName(obj); // hashcode node += " " + obj.GetHashCode(); // add level levels.Add(node); // follow parent CollectTreeUp(VisualTreeHelper.GetParent(obj), levels); }
/// <summary> /// Walks down the tree from the given object and collects tree information. /// </summary> private static void CollectTreeDown(DependencyObject obj, StringBuilder output, int level, Stack<int> treeLineIndexes, int propertyIntend, ref int visualElementCount) { if (obj == null) return; // count visual elements visualElementCount++; // prepare intend/treelines string intend = ""; for (int i = 0; i < level; i++) { if (treeLineIndexes.Contains(i) || i + 1 >= level) intend += " |"; else intend += " "; } // type name string typeName = GetTypeName(obj); output.Append(intend + "-" + typeName); output.Append(GetIntendString(propertyIntend - typeName.Length)); // XAML name output.Append(" " + GetElementName(obj)); // hashcode output.Append(" " + obj.GetHashCode()); output.Append(" " + GetProperties(obj)); output.Append(Environment.NewLine); // get child count int childCount = VisualTreeHelper.GetChildrenCount(obj); // remember treeline if more than one child if (childCount >= 2) treeLineIndexes.Push(level); int levelForChildTree = ++level; int lastChildIncludedInIntend = -1; int intendForChild = 0; for (int i = 0; i < childCount; i++) { // remove the treeline for last element if (childCount >= 2 && i + 1 == childCount) treeLineIndexes.Pop(); if (lastChildIncludedInIntend < i) lastChildIncludedInIntend = GetPropertyValueIntend(obj, i, out intendForChild); // follow the child DependencyObject child = VisualTreeHelper.GetChild(obj, i); CollectTreeDown(child, output, levelForChildTree, treeLineIndexes, intendForChild, ref visualElementCount); } }
/// <summary> /// Undoes the effects of the <see cref="ItemsControl.PrepareContainerForItemOverride(System.Windows.DependencyObject,System.Object)"/> method. /// </summary> /// <param name="element">The container element.</param> /// <param name="item">The item.</param> protected override void ClearContainerForItemOverride(DependencyObject element, object item) { base.ClearContainerForItemOverride(element, item); DataGridFacade.Instance.RemoveController(FooterCellController.FOOTER_PREFIX + element.GetHashCode()); }
/// <summary> /// Undoes the effects of the <see cref="ItemsControl.PrepareContainerForItemOverride(System.Windows.DependencyObject,System.Object)"/> method. /// </summary> /// <param name="element">The container element.</param> /// <param name="item">The item.</param> protected override void ClearContainerForItemOverride(DependencyObject element, object item) { base.ClearContainerForItemOverride(element, item); DataGridFacade.Instance.RemoveController(element.GetHashCode().ToString()); }
//+---------------------------------------------------------------------------------- // // ApplyTemplateContent // // Instantiate the content of the template (either from FEFs or from Baml). // This is done for every element to which this template is attached. // //+---------------------------------------------------------------------------------- #region InstantiateSubTree //[CodeAnalysis("AptcaMethodsShouldOnlyCallAptcaMethods")] //Tracking Bug: 29647 internal static bool ApplyTemplateContent( UncommonField<HybridDictionary[]> dataField, DependencyObject container, FrameworkElementFactory templateRoot, int lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate) { Debug.Assert(frameworkTemplate != null ); bool visualsCreated = false; FrameworkElement feContainer = container as FrameworkElement; // Is this a FEF-style template? if (templateRoot != null) { // Yes, we'll instantiate from a FEF tree. EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordXamlBaml, EventTrace.Level.Verbose, EventTrace.Event.WClientParseInstVisTreeBegin); CheckForCircularReferencesInTemplateTree(container, frameworkTemplate ); // Container is considered ChildIndex '0' (Self), but, // Container.ChildIndex isn't set List<DependencyObject> affectedChildren = new List<DependencyObject>(lastChildIndex); // Assign affectedChildren to container TemplatedFeChildrenField.SetValue(container, affectedChildren); // When building the template children chain, we keep a chain of // nodes that don't need to be in the chain for property invalidation // or lookup purposes. (And hence not assigned a TemplateChildIndex) // We only need them in order to clean up their _templatedParent // references (see FrameworkElement.ClearTemplateChain) List<DependencyObject> noChildIndexChildren = null; // Instantiate template // Setup container's reference to first child in chain // and add to tree DependencyObject treeRoot = templateRoot.InstantiateTree( dataField, container, container, affectedChildren, ref noChildIndexChildren, ref frameworkTemplate.ResourceDependents); Debug.Assert(treeRoot is FrameworkElement || treeRoot is FrameworkContentElement, "Root node of tree must be a FrameworkElement or FrameworkContentElement. This should have been caught by set_VisualTree" ); // From childFirst to childLast is the chain of child nodes with // childIndex. Append that chain with the chain of child nodes // with no childIndex assigned. if( noChildIndexChildren != null ) { affectedChildren.AddRange(noChildIndexChildren); } visualsCreated = true; if (feContainer != null && EventTrace.IsEnabled(EventTrace.Keyword.KeywordXamlBaml, EventTrace.Level.Verbose)) { string label = feContainer.Name; if (label == null || label.Length == 0) label = container.GetHashCode().ToString(System.Globalization.CultureInfo.InvariantCulture); EventTrace.EventProvider.TraceEvent(EventTrace.Event.WClientParseInstVisTreeEnd, EventTrace.Keyword.KeywordXamlBaml, EventTrace.Level.Verbose, String.Format(System.Globalization.CultureInfo.InvariantCulture, "Style.InstantiateSubTree for {0} {1}", container.GetType().Name, label)); } } // It's not a FEF-style template, is it a non-empty optimized one? else if (frameworkTemplate != null && frameworkTemplate.HasXamlNodeContent) { // Yes, create from the optimized template content. EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordXamlBaml, EventTrace.Level.Verbose, EventTrace.Event.WClientParseInstVisTreeBegin); CheckForCircularReferencesInTemplateTree(container, frameworkTemplate ); // Container is considered ChildIndex '0' (Self), but, // Container.ChildIndex isn't set List<DependencyObject> affectedChildren = new List<DependencyObject>(lastChildIndex); // Assign affectedChildren to container TemplatedFeChildrenField.SetValue(container, affectedChildren); DependencyObject treeRoot; // Load the content treeRoot = frameworkTemplate.LoadContent( container, affectedChildren); Debug.Assert(treeRoot == null || treeRoot is FrameworkElement || treeRoot is FrameworkContentElement, "Root node of tree must be a FrameworkElement or FrameworkContentElement. This should have been caught by set_VisualTree" ); visualsCreated = true; if (feContainer != null && EventTrace.IsEnabled(EventTrace.Keyword.KeywordXamlBaml, EventTrace.Level.Verbose)) { string label = feContainer.Name; if (label == null || label.Length == 0) label = container.GetHashCode().ToString(System.Globalization.CultureInfo.InvariantCulture); EventTrace.EventProvider.TraceEvent(EventTrace.Event.WClientParseInstVisTreeEnd, EventTrace.Keyword.KeywordXamlBaml, EventTrace.Level.Verbose, String.Format(System.Globalization.CultureInfo.InvariantCulture, "Style.InstantiateSubTree for {0} {1}", container.GetType().Name, label)); } } // No template was supplied. Allow subclasses to provide the template. // This is currently only implemented for FrameworkElement's. else { if (feContainer != null) { // This template will not be driven by the Style in any way. // Rather, it will be built and initialized by the callee // CALLBACK #if DEBUG Debug.Assert( feContainer._buildVisualTreeVerification == VerificationState.WaitingForBuildVisualTree, "The BuildVisualTree override has triggered another call to itself. This is not good - something between the two Style.InstantiateSubTree calls on the stack is doing A Very Bad Thing."); feContainer._buildVisualTreeVerification = VerificationState.WaitingForAddCustomTemplateRoot; bool exceptionThrown = true; try { #endif Debug.Assert(frameworkTemplate != null, "Only FrameworkTemplate has the ability to build a VisualTree by this means"); visualsCreated = frameworkTemplate.BuildVisualTree(feContainer); #if DEBUG exceptionThrown = false; } finally { if (!exceptionThrown) // results are unreliable if an exception was thrown { if( visualsCreated ) { Debug.Assert( feContainer._buildVisualTreeVerification == VerificationState.WaitingForBuildVisualTreeCompletion, "A derived class overriding BuildVisualTree must call AddCustomTemplateRoot to attach its custom subtree before exiting."); } else { Debug.Assert( feContainer._buildVisualTreeVerification == VerificationState.WaitingForAddCustomTemplateRoot, "If a derived class overriding BuildVisualTree has called AddCustomTemplateRoot to attach its custom subtree, its BuildVisualTree must return true to indicate that it has done so."); } } // All done building this visual tree, stand by for the next one. feContainer._buildVisualTreeVerification = VerificationState.WaitingForBuildVisualTree; } #endif } } return visualsCreated; }
public override int GetHashCode() { return(target.GetHashCode() ^ property.GetHashCode()); }