Canvas supports ScrollViewer.

프로그래밍 2010/07/15 17:45 Posted by 아일레프

 <ScrollViewer Height=100>

          <Canvas>

                   <TextBlock Canva.Top=120 Text=Test/>

          </Canvas>

</ScrollViewer>

 

VerticalScrollBar is visible in upper case? Unfortunately, it is invisible, because the Size of Canvas is zero-zero.

How we can see the VerticalScrollBar in upper case? 

The problem is caused by MeasureOverride method of Canvas. Below is an original MeasureOverride method of Canvas.

protected override Size MeasureOverride(Size constraint)

{

    Size availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);

    foreach (UIElement element in base.InternalChildren)

    {

        if (element != null)

        {

            element.Measure(availableSize);

        }

    }

    return new Size();

}

OK, then, solution can be very easy, just define new Canvas, and override MeasureOverride method like below.

        protected override Size MeasureOverride(Size constraint)

        {

            Size availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);

 

            double maxHeight = 0;

            double maxWidth = 0;

 

            foreach (UIElement element in base.InternalChildren)

            {

                if (element != null)

                {

                    element.Measure(availableSize);

                    double left = Canvas.GetLeft(element);

                    double top = Canvas.GetTop(element);

                    left += element.DesiredSize.Width;

                    top += element.DesiredSize.Height;

 

                    maxWidth = maxWidth < left ? left : maxWidth;

                    maxHeight = maxHeight < top ? top : maxHeight;

                }

            }

            return new Size { Height = maxHeight, Width = maxWidth };

        }

 

Thank you,

 

** Thinking Outside the Grid 
refered
referred the difference between Grid and Canvas.

The difference between the Canvas and the single-cell Grid is in how the container appears to the rest of the layout system. WPF and Silverlight incorporate a two-pass, top-down layout where every element interrogates the size of its children and is then responsible for arranging its children relative to itself. Within this layout system, the Canvas and the single-cell Grid are very different:
1. To its children, the Grid has the same dimensions as the dimensions of its own parent. These are usually finite dimensions, but the Canvas always appears to have infinite dimensions to its children.
2. The Grid reports the composite size of its children to its parent. 
However, the Canvas always has an apparent size of zero, regardless of the children it contains.

저작자 표시


 

티스토리 툴바