Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Conceptual Children: A powerful new concept in WPF

, 6 Apr 2008
This article describes a new approach by which an element can remove its visual and logical relationships to its children while maintaining a conceptual parental relationship with those children.
using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace WpfDiscipleBlogViewer3D
{
    /// <summary>
    /// Hosts an Viewport3D in the adorner layer above a Panel3D.
    /// </summary>
    public class Panel3DAdorner : Adorner
    {
        #region Data

        private ArrayList _logicalChildren;
        private readonly DockPanel _viewportHost = new DockPanel();

        #endregion // Data

        #region Constructor

        public Panel3DAdorner(Panel3D adornedPanel3D, Viewport3D viewport)
            : base(adornedPanel3D)
        {
            _viewportHost.Children.Add(viewport);
            _viewportHost.Background = Brushes.Transparent;
            base.AddLogicalChild(_viewportHost);
            base.AddVisualChild(_viewportHost);
        }

        #endregion // Constructor

        #region Measure/Arrange

        /// <summary>
        /// Allows the control to determine how big it wants to be.
        /// </summary>
        /// <param name="constraint">A limiting size for the control.</param>
        protected override Size MeasureOverride(Size constraint)
        {
            //_viewport.Measure(constraint);
            //return _viewport.DesiredSize;
            return constraint;
        }

        /// <summary>
        /// Positions and sizes the control.
        /// </summary>
        /// <param name="finalSize">The actual size of the control.</param>		
        protected override Size ArrangeOverride(Size finalSize)
        {
            Rect rect = new Rect(new Point(), finalSize);

            _viewportHost.Arrange(rect);

            return finalSize;
        }

        #endregion // Measure/Arrange

        #region Visual Children

        /// <summary>
        /// Required for the element to be rendered.
        /// </summary>
        protected override int VisualChildrenCount
        {
            get { return 1; }
        }

        /// <summary>
        /// Required for the element to be rendered.
        /// </summary>
        protected override Visual GetVisualChild(int index)
        {
            if (index != 0)
                throw new ArgumentOutOfRangeException("index");

            return _viewportHost;
        }

        #endregion // Visual Children

        #region Logical Children

        /// <summary>
        /// Required for the displayed element to inherit property values
        /// from the logical tree, such as FontSize.
        /// </summary>
        protected override IEnumerator LogicalChildren
        {
            get
            {
                if (_logicalChildren == null)
                {
                    _logicalChildren = new ArrayList();
                    _logicalChildren.Add(_viewportHost);
                }

                return _logicalChildren.GetEnumerator();
            }
        }

        #endregion // Logical Children
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License

Share

About the Author

Dr. WPF

United States United States
Dr. WPF is a WPF Disciple! Check out the doctor's blog and bio for more information.

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 6 Apr 2008
Article Copyright 2008 by Dr. WPF
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid