开发者

Can someone please explain what Child.Measure(availableSize) does in silverlight?

    protected override Size MeasureOverride(Size availableSiz开发者_如何转开发e)
    {

        foreach (UIElement child in Children)
        {
            child.Measure(availableSize);
            //you get the desired size after

        }


    }

So for brevity i cut the code short, but i am having trouble understanding why in some example they pass availableSize into the child then after you get the DesiredHeight, DesiredWidth property. What does Measure actually measure and why do i even have to pass anything in? I have read msdn and did examples and still not understanding or maybe i am thinking this too much.


Controls are laying out in the WPF as a Tree strcture (VisualTree). 'Panel' is a type of control which can hold many child UIElements in it or in other words Panels are used as the LayoutSystem in WPF/Silverlight. Framework given panels are StackPanel,Grid,Canvas etc. And if you derive a class from Panel and override MeasureOverride() and ArrangeOverride() You will be able to make custom panels.

Ok now the answer to the question. In a custom panel or control you can call child.Measure(availableSize) to let the all the next level children to know that 'available'size is what 'child' has got now. And giving an opportunity to decide those child elements to fit in it. It is like I tell my children that this is the space we got decide your sizes. So once a control calls Measure() it passes that to the subsequent children and each of them will call its own measure() till the end of the VisualTree and once that call finishes you will be able to see the desired size child wanted in child.DesiredSize. Now that you got all children's desiredSizes you can compute the desiredsize for the control and return that at the end of the MeasureOverride() function

The Layout mechanism works in a two pass, Measure and Arrange. Once you measure your children with the availablesize you can arrange your children because at that momement you will know what size each of the children need from the child.DesiredSize


My guess from reading the docs would be that the AvailableSize is used for constraining the size of the underlying controls that make up the target control. That way they can collapse if the available size is to small, or only grow to a max size (regardless of how large the target area is).

It would be possible in this context to also "error" if you could not fit in the available area, though the usefulness of that might be limited.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜