AffectsMeasure or AffectsArrange

2019-02-26 14:01发布

I am creating the custom control where i need to update the layout if one of the Dependency property changes. I can use the FrameworkMetadataProperty.AffectsMeasure or FrameworkMetadataProperty.AffectsArrange.

Q.1 - I am confused which one to use.

Moreover I can also use the UpdateLayout and InvalidateVisual methods as well in order to update the UI.

Q.2 - All these 4 things looks similar and am confused on which one to use when?

标签: c# wpf layout
1条回答
Juvenile、少年°
2楼-- · 2019-02-26 14:29

First, the difference between setting FrameworkPropertyMetadataOptions and calling methods as UpdateLayout or InvalidateVisual is obvious. In the latter case you call these methods in your control code, wheras in the former case the appropriate methods are called by the framework.

The difference between AffectsMeasure and AffectsArrange is simply that one results in a call to UIElement.InvalidateMeasure ("affects the measure pass of the layout") and the other results in a call to UIElement.InvalidateArrange ("affects the arrange pass of the layout").

The difference becomes clear from the Remarks in UIElement.Measure:

When a layout is first instantiated, it always receives a Measure call before Arrange. However, after the first layout pass, it may receive an Arrange call without a Measure; this can happen when a property that affects only Arrange is changed (such as alignment), or when the parent receives an Arrange without a Measure. A Measure call will automatically invalidate an Arrange call.

and from the Remarks in UIElement.InvalidateMeasure:

Calling this method also calls InvalidateArrange internally, there is no need to call InvalidateMeasure and InvalidateArrange in succession


UPDATE: For the difference between UpdateLayout and InvalidateVisual, see the Remarks in UpdateLayout:

When you call this method, elements with IsMeasureValid false or IsArrangeValid false will call element-specific MeasureCore and ArrangeCore methods, which forces layout update, and all computed sizes will be validated.

Calling this method has no effect if layout is unchanged, or if neither arrangement nor measurement state of a layout is invalid. However, if layout is invalid in either respect, the UpdateLayout call will redo the entire layout. Therefore, you should avoid calling UpdateLayout after each incremental and minor change in the element tree. The layout system will perform element layout in a deferred manner, using an algorithm that balances performance and currency, and with a weighting strategy to defer changes to roots until all child elements are valid. You should only call UpdateLayout if you absolutely need updated sizes and positions, and only after you are certain that all changes to properties that you control and that may affect layout are completed.

and in InvalidateVisual:

This method calls InvalidateArrange internally.

This method is not generally called from your application code. The WPF framework-level layout system does its own handling of changes in the visual tree of an element, and would be calling the equivalent of this method when necessary already. Calling this method is necessary only for advanced scenarios. One such advanced scenario is if you are creating a PropertyChangedCallback for a dependency property that is not on a Freezable or FrameworkElement derived class that still influences the layout when it changes.

查看更多
登录 后发表回答