在Avalonia UI中,“FrameworkPropertyMetadataOptions.AffectsParentArrange”是一个枚举值,用于指示当依赖属性的值更改时,该属性是否会影响父控件的布局。如果属性的值更改会影响父级控件的布局,则可以将此选项设置为true。
以下是一个示例,在此示例中,“MyCustomControl”的大小由两个依赖属性控制:“Width”和“Height”。设置“FrameworkPropertyMetadataOptions.AffectsParentArrange”选项后,当这些属性的值更改时,该控件的“ArrangeOverride”方法将被调用,该方法将更新父级控件的布局。
public class MyCustomControl : Control
{
public static readonly DirectProperty WidthProperty =
AvaloniaProperty.RegisterDirect(
nameof(Width),
o => o.Width,
(o, v) => o.Width = v,
0.0,
FrameworkPropertyMetadataOptions.AffectsParentArrange);
public static readonly DirectProperty HeightProperty =
AvaloniaProperty.RegisterDirect(
nameof(Height),
o => o.Height,
(o, v) => o.Height = v,
0.0,
FrameworkPropertyMetadataOptions.AffectsParentArrange);
private double _width;
private double _height;
public double Width
{
get { return _width; }
set { SetAndRaise(WidthProperty, ref _width, value); }
}
public double Height
{
get { return _height; }
set { SetAndRaise(HeightProperty, ref _height, value); }
}
public override Size ArrangeOverride(Size finalSize)
{
// Perform custom layout logic here.
// ...
// Call base implementation last to update parent layout.
return base.ArrangeOverride(finalSize);
}
}