This provides some relative units and features for Avalonia.
- Now support using relatives in style setters.
<Style Selector="Button">
<Setter Property="r:Relative.Width" Value="20pw"/>
</Style>- Now you can use a visual anchor to provide relative feature for objects which is non-visual but have layout properties.
dotnet add package RelativeControl.Avalonia<CONTROL r:Relative.Width="20pw"/>This will set the
CONTROL's width to 20% of its logical parent's width.You can also add / subtract the values:
<CONTROL r:Relative.Width="20pw+10ph"/>This will set the
CONTROL's width to :20% logical parent width + 10% logical parent height.
You can also add / subtract / multiply / divide the value at code behind.
- Multiply() and Divide() will affect the instance and all its references.
- * or / operation will create a light copy and will not affect the instance.
Relative.SetOneTimeWidthandRelative.SetOneTimeHeightwill update only once when the control is attached to visual tree.
Other properties using RelativeLength / RelativeLengthMerge:
- Relative.Height
- Relative.MinWidth
- Relative.MinHeight
- Relative.MaxWidth
- Relative.MaxHeight
- Relative.SetOneTimeWidth
- Relative.SetOneTimeHeight
<CONTROL r:Relative.CornerRadius="10sw 10sw+5sh 10sh-5sw 10sh"/>This will set the
CONTROL'sCornerRadiusto:TopLeft = 10% width,
TopRight = 10% width + 5% height,
BottomRight = 10% height - 5% width,
BottomLeft = 10% height
<CONTROL r:Relative.BorderThickness="1em 2em-5px"/>This will set the
CONTROL's BorderThickness to:Horizontal(Left,Right) = 1x FontSize,
Vertical(Top,Bottom) = 2x FontSize - 5px
Other properties using RelativeThickness:
- Relative.Margin
- Relative.Padding
<CONTROL PROPERTY="{r:RelativeBinding {Binding SOURCE_PROPERTY},50%}"/>This will set the
PROPERTY's value to 50% ofSOURCE_PROPERTY's value.A valid
SourceProperty's value must be:
- a double
- any value that can convert to double (like a number string)
- any custom structs or classes that inherits
IMulDiv<RelativeScale>orIMulDiv<double>.
RelativeBindOneTimewill update only once when the control is attached to visual tree.
public static readonly StyledProperty<IRelative<T>> XXXProperty =
AvaloniaProperty.Register<..., IRelative<T>>(nameof(XXX));public static readonly DirectProperty<..., IRelative<T>> XXXProperty =
AvaloniaProperty.RegisterDirect<..., IRelative<T>>(...);public static readonly AttachedProperty<IRelative<T>> XXXProperty =
AvaloniaProperty.RegisterAttached<...,...,IRelative<T>>(...);px: Device-independent Pixel(1/96th of an inch)
cm: Centimeter
mm: Millimeter
in: Inch
tpw: TemplatedParent's width
tph: TemplatedParent's height
lpw or pw: LogicalParent's width
lph or ph: LogicalParent's height
vpw: VisualParent's width
vph: VisualParent's height
sw: The control itself's width
sh: The control itself's height
em: The control's FontSize
vw: Window's width
vh: Window's height
%: Represents percentage. Only used for custom bindings.
- Width
- Height
- MinWidth
- MinHeight
- MaxWidth
- MaxHeight
- BorderThickness
- CornerRadius
- Margin
- Padding
- RelativeBinding
You can bind any property! How to bind a custom property?
- SetOneTimeWidth
- SetOneTimeHeight
- RelativeBindOneTime
These will update only once when the control is attached to visual tree!
Relative units are using percentages (Excepts em). To make it more like css.
- <Button r:Relative.Width="0.5vw"/>
+ <Button r:Relative.Width="50vw"/>Move all relative length from Units.cs to RelativeLength.cs
+ public abstract class RelativeLengthBase
- RelativeLength.RelativeLengthChanged (Rename)
+ RelativeLengthBase.RelativeLengthChanged
- RelativeLengthBase.OnRelativeLengthChanged (Rename)
+ RelativeLengthBase.RelativeLengthChanged
- RelativeMerge.Multiplier (Rename)
+ RelativeLengthCollection.Scaler
- SetTarget- RelativeThicknessChanged (Rename)
+ RelativeThicknessChangedHandler
- OnRelativeThicknessChanged (Rename)
+ RelativeThicknessChanged
- SetTarget- RelativeSizeChanged (Rename)
+ RelativeSizeChangedHandler
- OnRelativeSizeChanged (Rename)
+ RelativeSizeChanged
- SetTarget- RelativeCornerRadiusChanged (Rename)
+ RelativeCornerRadiusChangedHandler
- OnRelativeCornerRadiusChanged (Rename)
+ RelativeCornerRadiusChanged
- SetTarget- public event RelativeXXXChanged(T oldValue,T newValue);
+ public event RelativeChanged<T>(IRelative<T> sender,RelativeChangedEventArgs<T> args);
+ public class RelativeChangedEventArgs<T>(T oldValue, T newValue) : RelativeChangedEventArgs {
+ public readonly T OldValue = oldValue;
+ public readonly T NewValue = newValue;
+ }