feat: add new readonly property: LineCount. add a demo of consuming this property. demo will be reverted.

This commit is contained in:
rabbitism
2025-07-13 15:32:33 +08:00
parent 0a684ec70f
commit ff65f4f441
2 changed files with 19 additions and 7 deletions

View File

@@ -104,11 +104,19 @@
<TabItem Header="Flat"> <TabItem Header="Flat">
<TabControl> <TabControl>
<TabItem Header="Common"> <TabItem Header="Common">
<u:ElasticWrapPanel IsFillHorizontal="{Binding IsFillHorizontal}" <u:ElasticWrapPanel
IsFillVertical="{Binding IsFillVertical}" IsFillVertical="{Binding IsFillVertical}"
ItemHeight="{Binding ItemHeight}" ItemHeight="{Binding ItemHeight}"
ItemWidth="{Binding ItemWidth}" ItemWidth="{Binding ItemWidth}"
Orientation="{Binding SelectedOrientation}"> Orientation="{Binding SelectedOrientation}">
<u:ElasticWrapPanel.Styles>
<Style Selector="u|ElasticWrapPanel">
<Setter Property="IsFillHorizontal" Value="True" />
</Style>
<Style Selector="u|ElasticWrapPanel[LineCount=1]">
<Setter Property="IsFillHorizontal" Value="False" />
</Style>
</u:ElasticWrapPanel.Styles>
<Border Background="{DynamicResource SemiRed5Color}" /> <Border Background="{DynamicResource SemiRed5Color}" />
<Border Background="{DynamicResource SemiPink5Color}" /> <Border Background="{DynamicResource SemiPink5Color}" />
<Border Background="{DynamicResource SemiPurple5Color}" /> <Border Background="{DynamicResource SemiPurple5Color}" />

View File

@@ -11,10 +11,8 @@ public class ElasticWrapPanel : WrapPanel
{ {
static ElasticWrapPanel() static ElasticWrapPanel()
{ {
IsFillHorizontalProperty.Changed.AddClassHandler<Control>(OnIsFillPropertyChanged);
IsFillVerticalProperty.Changed.AddClassHandler<Control>(OnIsFillPropertyChanged);
AffectsMeasure<ElasticWrapPanel>(IsFillHorizontalProperty, IsFillVerticalProperty); AffectsMeasure<ElasticWrapPanel>(IsFillHorizontalProperty, IsFillVerticalProperty);
AffectsArrange<ElasticWrapPanel>(IsFillHorizontalProperty, IsFillVerticalProperty);
} }
#region AttachedProperty #region AttachedProperty
@@ -60,9 +58,15 @@ public class ElasticWrapPanel : WrapPanel
public static readonly StyledProperty<bool> IsFillVerticalProperty = public static readonly StyledProperty<bool> IsFillVerticalProperty =
AvaloniaProperty.Register<ElasticWrapPanel, bool>(nameof(IsFillVertical)); AvaloniaProperty.Register<ElasticWrapPanel, bool>(nameof(IsFillVertical));
private static void OnIsFillPropertyChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e) private int _lineCount;
public static readonly DirectProperty<ElasticWrapPanel, int> LineCountProperty = AvaloniaProperty.RegisterDirect<ElasticWrapPanel, int>(
nameof(LineCount), o => o.LineCount);
public int LineCount
{ {
(d as ElasticWrapPanel)?.InvalidateMeasure(); get => _lineCount;
private set => SetAndRaise(LineCountProperty, ref _lineCount, value);
} }
#endregion #endregion
@@ -376,7 +380,7 @@ public class ElasticWrapPanel : WrapPanel
lineUIEles.Clear(); lineUIEles.Clear();
} }
} }
LineCount = lineUVCollection.Count;
lineUVCollection.ForEach(col => col.Dispose()); lineUVCollection.ForEach(col => col.Dispose());
lineUVCollection.Clear(); lineUVCollection.Clear();
return finalSize; return finalSize;