MeasureOverride not always called on children's property changes

二次信任 提交于 2019-12-24 13:34:51

问题


I wrote a panel similar to the built-in StackPanel. I works almost fine except for a slight problem:

Changing layout properties on children do not always cause the panel's MeasureOverride and ArrangeOverride to be called. They are always called when a child's Visibility property changes, but not when the Width and Height properties change.

I haven't yet managed to reproduce this behavior in a sample small enough to be appropriate for being included in a question on StackOverflow: But since it works fine in the trivial sample I made, I know I must do something avoidable in my actual panel.

So my question: In which circumstances does an element not invalidate its parents measure when changing size-related properties?

I tag this wpf also (I used Silverlight) to have a broader audience - I suspect this will apply to both xaml implementations equally.


回答1:


I figured what my mistake was and under which condition the panel's MeasureOverride is no longer called on certain changes for size-related properties.

My panel called Measure on children with the exact size the children should have, rather on the size of the panel.

A panel doesn't get it's MeasureOverride method called when children begin to desire more space than was told to them is available in the last Measure call - which makes sense.

Summary: The parameter for the Measure method you call on a child must denote the space the parent panel allots to all children, not just the one Measure is called on.




回答2:


You must make sure you call the base methods MeasureOverride.

    protected override Size MeasureOverride(Size availableSize)
    {
        // you must call this
        var throwaway = base.MeasureOverride(availableSize);

        // your code here

        return yourNewSize;
    }


来源:https://stackoverflow.com/questions/20766739/measureoverride-not-always-called-on-childrens-property-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!