Browse Source

Add Points binding observability support for Polygon and Polyline

pull/15030/head
SKProCH 2 years ago
parent
commit
e16d987945
  1. 1
      src/Avalonia.Controls/Shapes/Polygon.cs
  2. 1
      src/Avalonia.Controls/Shapes/Polyline.cs
  3. 35
      src/Avalonia.Controls/Shapes/Shape.cs

1
src/Avalonia.Controls/Shapes/Polygon.cs

@ -12,6 +12,7 @@ namespace Avalonia.Controls.Shapes
static Polygon()
{
AffectsGeometry<Polygon>(PointsProperty);
CollectionAffectsGeometry<Polygon>(PointsProperty);
}
public Polygon()

1
src/Avalonia.Controls/Shapes/Polyline.cs

@ -13,6 +13,7 @@ namespace Avalonia.Controls.Shapes
{
StrokeThicknessProperty.OverrideDefaultValue<Polyline>(1);
AffectsGeometry<Polyline>(PointsProperty);
CollectionAffectsGeometry<Polyline>(PointsProperty);
}
public Polyline()

35
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
});
}
}
/// <summary>
/// Marks a property as a collection whose elements affecting the shape's geometry.
/// </summary>
/// <param name="properties">The properties.</param>
/// <remarks>
/// After a call to this method in a control's static constructor, any change to the
/// property will cause <see cref="InvalidateGeometry"/> to be called on the element.
/// </remarks>
protected static void CollectionAffectsGeometry<TShape>(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();
}
/// <summary>
/// Creates the shape's defining geometry.

Loading…
Cancel
Save