Ejemplo n.º 1
0
        /// <summary>
        /// Getter for <see cref="AttachedVisualsProperty"/>
        /// </summary>
        public static VisualAttachmentCollection <IAttachable> GetAttachedVisuals(DependencyObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            var collection = (VisualAttachmentCollection <IAttachable>)obj.GetValue(AttachedVisualsProperty);

            // If the collection wasn't yet set
            if (collection == null)
            {
                // Create a new instance
                collection = new VisualAttachmentCollection <IAttachable>();

                // And set it for the object
                obj.SetValue(AttachedVisualsProperty, collection);

                if (obj is FrameworkElement element)
                {
                    // Subscribe to loaded and unloaded events
                    element.Loaded   -= FrameworkElementLoaded;
                    element.Loaded   += FrameworkElementLoaded;
                    element.Unloaded -= FrameworkElementUnloaded;
                    element.Unloaded += FrameworkElementUnloaded;
                }
            }

            return(collection);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Setter for <see cref="AttachedVisualsProperty"/>
        /// </summary>
        public static void SetAttachedVisuals(DependencyObject obj, VisualAttachmentCollection <IAttachable> value)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            obj.SetValue(AttachedVisualsProperty, value);
        }