From e16d987945cc717bf0ffbf80902e052dd84b3665 Mon Sep 17 00:00:00 2001 From: SKProCH Date: Mon, 18 Mar 2024 23:21:11 +0300 Subject: [PATCH] Add Points binding observability support for Polygon and Polyline --- src/Avalonia.Controls/Shapes/Polygon.cs | 1 + src/Avalonia.Controls/Shapes/Polyline.cs | 1 + src/Avalonia.Controls/Shapes/Shape.cs | 35 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/src/Avalonia.Controls/Shapes/Polygon.cs b/src/Avalonia.Controls/Shapes/Polygon.cs index e51e117e33..f8728354b7 100644 --- a/src/Avalonia.Controls/Shapes/Polygon.cs +++ b/src/Avalonia.Controls/Shapes/Polygon.cs @@ -12,6 +12,7 @@ namespace Avalonia.Controls.Shapes static Polygon() { AffectsGeometry(PointsProperty); + CollectionAffectsGeometry(PointsProperty); } public Polygon() diff --git a/src/Avalonia.Controls/Shapes/Polyline.cs b/src/Avalonia.Controls/Shapes/Polyline.cs index 84fccb66f1..ec933e391a 100644 --- a/src/Avalonia.Controls/Shapes/Polyline.cs +++ b/src/Avalonia.Controls/Shapes/Polyline.cs @@ -13,6 +13,7 @@ namespace Avalonia.Controls.Shapes { StrokeThicknessProperty.OverrideDefaultValue(1); AffectsGeometry(PointsProperty); + CollectionAffectsGeometry(PointsProperty); } public Polyline() diff --git a/src/Avalonia.Controls/Shapes/Shape.cs b/src/Avalonia.Controls/Shapes/Shape.cs index e358656fae..345e8108d1 100644 --- a/src/Avalonia.Controls/Shapes/Shape.cs +++ b/src/Avalonia.Controls/Shapes/Shape.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Specialized; using Avalonia.Collections; using Avalonia.Media; using Avalonia.Media.Immutable; @@ -219,6 +220,40 @@ namespace Avalonia.Controls.Shapes }); } } + + /// + /// Marks a property as a collection whose elements affecting the shape's geometry. + /// + /// The properties. + /// + /// After a call to this method in a control's static constructor, any change to the + /// property will cause to be called on the element. + /// + protected static void CollectionAffectsGeometry(params AvaloniaProperty[] properties) + where TShape : Shape + { + foreach (var property in properties) + { + property.Changed.Subscribe(e => + { + if (e.Sender is not TShape shape) return; + if (e.OldValue is INotifyCollectionChanged oldCollection) + { + oldCollection.CollectionChanged -= shape.OnCollectionThatAffectsGeometryChanged; + } + + if (e.NewValue is INotifyCollectionChanged newCollection) + { + newCollection.CollectionChanged += shape.OnCollectionThatAffectsGeometryChanged; + } + }); + } + } + + private void OnCollectionThatAffectsGeometryChanged(object? sender, NotifyCollectionChangedEventArgs e) + { + InvalidateGeometry(); + } /// /// Creates the shape's defining geometry.