Search code examples
avalonia

FrameworkPropertyMetadataOptions.AffectsMeasure and FrameworkPropertyMetadataOptions.AffectsRender in Avalonia


Moving the app under wpf to avalonia ui. During the transfer, I encountered something that is not in the documentation. In General there is a piece of code from wpf:

public class BaseLabelElement : UIElement
{
    public static readonly DependencyProperty FontFamilyProperty = DependencyProperty.Register(
        "FontFamily", typeof(FontFamily), typeof(BaseLabelElement), new FrameworkPropertyMetadata(DefaultFontFamily,
            FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, FontFamilyPropertyChanged));

    public FontFamily FontFamily
    {
        get { return (FontFamily)GetValue(FontFamilyProperty); }
        set { SetValue(FontFamilyProperty, value); }
    }
}

How to add a new behavior corresponding to

new FrameworkPropertyMetadata (.., FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender,..)

for the dependency property in Avalonia?


Solution

  • AffectsRender<T>(AvaloniaProperty[]) and AffectsMeasure<T>(AvaloniaProperty[]) methods in class Visual. These methods should be called in the static control constructor with each control property that should cause redrawing when modified. This is similar to the FrameworkPropertyMetadata.AffectsRender and AffectsMeasure flags in WPF.