c# - AffectsMeasure 或 AffectsArrange

标签 c# wpf layout

我正在创建自定义控件,如果其中一个依赖属性发生更改,我需要在其中更新布局。我可以使用 FrameworkMetadataProperty.AffectsMeasureFrameworkMetadataProperty.AffectsArrange

Q.1 - 我不知道该用哪个。

此外,我还可以使用 UpdateLayout 和 InvalidateVisual 方法来更新 UI。

Q.2 - 所有这 4 个东西看起来都很相似,不知道何时使用哪一个?

最佳答案

一、设置FrameworkPropertyMetadataOptions的区别并且将方法调用为 UpdateLayout 或 InvalidateVisual 是显而易见的。在后一种情况下,您在控制代码中调用这些方法,而在前一种情况下,适当的方法由框架调用。

AffectsMeasureAffectsArrange 之间的区别很简单,一个结果调用UIElement.InvalidateMeasure。 (“影响布局的测量 channel ”)和另一个调用 UIElement.InvalidateArrange 的结果(“影响布局的排列 channel ”)。

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.

来自UIElement.InvalidateMeasure中的备注:

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


更新:关于UpdateLayout 和InvalidateVisual 的区别,参见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.

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.

关于c# - AffectsMeasure 或 AffectsArrange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14414083/

相关文章:

c# - 如何为 xml 文件指定正确的位置?

c# - 如何检测和计算螺旋的圈数

WPF MVVM TreeView SelectedItem

python - 为什么 PyQt 的 pyuic 会忽略默认边距?

java - 如何将 JButton 放置在 x、y 位置

android - 改变涟漪效应的颜色

c# - 在 C# 中是否有 LZSS 压缩的公共(public)实现或者可以在 C# 中使用?

c# - 在公开 Begin/End 方法的类中实现 IDisposable

c# - IObservable - 如何发送/发布/推送新值到集合

c# - 具有自定义和默认先决条件的 Clickonce WPF 应用程序