示例#1
0
        /// <summary>
        /// Determines whether additional overflow columns are needed and if existing columns can
        /// be removed.
        /// </summary>
        /// <param name="availableSize">The size of the space available, used to constrain the
        /// number of additional columns that can be created.</param>
        /// <returns>The resulting size of the original content plus any extra columns.</returns>
        protected override Size MeasureOverride(Size availableSize)
        {
            if (RichTextContent == null)
            {
                return(new Size(0, 0));
            }

            // Make sure the RichTextBlock is a child, using the lack of
            // a list of additional columns as a sign that this hasn't been
            // done yet
            if (_overflowColumns == null)
            {
                Children.Add(RichTextContent);
                _overflowColumns = new List <RichTextBlockOverflow>();
            }

            // Start by measuring the original RichTextBlock content
            RichTextContent.Measure(availableSize);
            var maxWidth    = RichTextContent.DesiredSize.Width;
            var maxHeight   = RichTextContent.DesiredSize.Height;
            var hasOverflow = RichTextContent.HasOverflowContent;

            // Make sure there are enough overflow columns
            int overflowIndex = 0;

            while (hasOverflow && maxWidth < availableSize.Width && ColumnTemplate != null)
            {
                // Use existing overflow columns until we run out, then create
                // more from the supplied template
                RichTextBlockOverflow overflow;
                if (_overflowColumns.Count > overflowIndex)
                {
                    overflow = _overflowColumns[overflowIndex];
                }
                else
                {
                    overflow = (RichTextBlockOverflow)ColumnTemplate.LoadContent();
                    _overflowColumns.Add(overflow);
                    Children.Add(overflow);
                    if (overflowIndex == 0)
                    {
                        RichTextContent.OverflowContentTarget = overflow;
                    }
                    else
                    {
                        _overflowColumns[overflowIndex - 1].OverflowContentTarget = overflow;
                    }
                }

                // Measure the new column and prepare to repeat as necessary
                overflow.Measure(new Size(availableSize.Width - maxWidth, availableSize.Height));
                maxWidth   += overflow.DesiredSize.Width;
                maxHeight   = Math.Max(maxHeight, overflow.DesiredSize.Height);
                hasOverflow = overflow.HasOverflowContent;
                overflowIndex++;
            }

            // Disconnect extra columns from the overflow chain, remove them from our private list
            // of columns, and remove them as children
            if (_overflowColumns.Count > overflowIndex)
            {
                if (overflowIndex == 0)
                {
                    RichTextContent.OverflowContentTarget = null;
                }
                else
                {
                    _overflowColumns[overflowIndex - 1].OverflowContentTarget = null;
                }
                while (_overflowColumns.Count > overflowIndex)
                {
                    _overflowColumns.RemoveAt(overflowIndex);
                    Children.RemoveAt(overflowIndex + 1);
                }
            }

            // Report final determined size
            return(new Size(maxWidth, maxHeight));
        }
        protected override Size MeasureOverride(Size availableSize)
        {
            if (RichTextContent == null)
            {
                return(new Size(0, 0));
            }

            if (_overflowColumns == null)
            {
                Children.Add(RichTextContent);
                _overflowColumns = new List <RichTextBlockOverflow>();
            }

            RichTextContent.Measure(availableSize);
            var maxWidth    = RichTextContent.DesiredSize.Width;
            var maxHeight   = RichTextContent.DesiredSize.Height;
            var hasOverflow = RichTextContent.HasOverflowContent;

            var overflowIndex = 0;

            while (hasOverflow && maxWidth < availableSize.Width && ColumnTemplate != null)
            {
                RichTextBlockOverflow overflow;
                if (_overflowColumns.Count > overflowIndex)
                {
                    overflow = _overflowColumns[overflowIndex];
                }
                else
                {
                    overflow = (RichTextBlockOverflow)ColumnTemplate.LoadContent();
                    _overflowColumns.Add(overflow);
                    Children.Add(overflow);
                    if (overflowIndex == 0)
                    {
                        RichTextContent.OverflowContentTarget = overflow;
                    }
                    else
                    {
                        _overflowColumns[overflowIndex - 1].OverflowContentTarget = overflow;
                    }
                }

                overflow.Measure(new Size(availableSize.Width - maxWidth, availableSize.Height));
                maxWidth   += overflow.DesiredSize.Width;
                maxHeight   = Math.Max(maxHeight, overflow.DesiredSize.Height);
                hasOverflow = overflow.HasOverflowContent;
                overflowIndex++;
            }

            if (_overflowColumns.Count > overflowIndex)
            {
                if (overflowIndex == 0)
                {
                    RichTextContent.OverflowContentTarget = null;
                }
                else
                {
                    _overflowColumns[overflowIndex - 1].OverflowContentTarget = null;
                }
                while (_overflowColumns.Count > overflowIndex)
                {
                    _overflowColumns.RemoveAt(overflowIndex);
                    Children.RemoveAt(overflowIndex + 1);
                }
            }

            return(new Size(maxWidth, maxHeight));
        }