Example #1
0
        protected override Size MeasureOverride(Size availableSize)
        {
            if (Display != null)
            {
                float clientW = (float)Display.ClientWidth;
                float clientH = (float)Display.ClientHeight;

                SizeToContent sizeToContent = SizeToContent;
                if (sizeToContent == SizeToContent.Manual)
                {
                    base.MeasureOverride(new Size(clientW, clientH));
                    return(new Size(Width, Height));
                }
                else
                {
                    // calculate content desired size
                    Size constraint = new Size(
                        sizeToContent == SizeToContent.Height ? clientW : float.PositiveInfinity,
                        sizeToContent == SizeToContent.Width ? clientH : float.PositiveInfinity);

                    Size desiredSize = base.MeasureOverride(constraint);

                    // adjust content size to window size (including borders)
                    int desiredWidth  = (int)Math.Ceiling(desiredSize.Width);
                    int desiredHeight = (int)Math.Ceiling(desiredSize.Height);
                    Display.AdjustWindowSize(ref desiredWidth, ref desiredHeight);

                    // update window to the desired size
                    if (sizeToContent != SizeToContent.Height)
                    {
                        Width = desiredWidth;
                    }
                    else
                    {
                        desiredWidth = (int)Width;
                    }

                    if (sizeToContent != SizeToContent.Width)
                    {
                        Height = desiredHeight;
                    }
                    else
                    {
                        desiredHeight = (int)Height;
                    }

                    return(new Size(desiredWidth, desiredHeight));
                }
            }

            return(base.MeasureOverride(availableSize));
        }
Example #2
0
        private Size MeasureContent()
        {
            // Make sure window is already initialized and template is applied
            InitObject();
            ApplyTemplate();

            // Measure window content
            UIElement child = VisualTreeHelper.GetChild(this, 0) as UIElement;

            if (child != null)
            {
                child.Measure(new Size(float.PositiveInfinity, float.PositiveInfinity));

                Size desiredSize = child.DesiredSize;

                int width  = (int)Math.Ceiling(desiredSize.Width);
                int height = (int)Math.Ceiling(desiredSize.Height);
                Display.AdjustWindowSize(ref width, ref height);

                return(new Size(width, height));
            }

            return(new Size(0.0f, 0.0f));
        }