| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using Avalonia.Controls.Shapes;
- using Avalonia.Media;
- using Avalonia.Metadata;
- namespace Avalonia.Controls
- {
- public class DrawingPresenter : Control
- {
- public static readonly StyledProperty<Drawing> DrawingProperty =
- AvaloniaProperty.Register<DrawingPresenter, Drawing>(nameof(Drawing));
- [Content]
- public Drawing Drawing
- {
- get => GetValue(DrawingProperty);
- set => SetValue(DrawingProperty, value);
- }
- public static readonly StyledProperty<Stretch> StretchProperty =
- AvaloniaProperty.Register<DrawingPresenter, Stretch>(nameof(Stretch), Stretch.Uniform);
- public Stretch Stretch
- {
- get => GetValue(StretchProperty);
- set => SetValue(StretchProperty, value);
- }
- static DrawingPresenter()
- {
- AffectsMeasure(DrawingProperty);
- AffectsRender(DrawingProperty);
- }
- private Matrix _transform = Matrix.Identity;
- protected override Size MeasureOverride(Size availableSize)
- {
- if (Drawing == null) return new Size();
- var (size, transform) = Shape.CalculateSizeAndTransform(availableSize, Drawing.GetBounds(), Stretch);
- _transform = transform;
- return size;
- }
- public override void Render(DrawingContext context)
- {
- if (Drawing != null)
- {
- using (context.PushPreTransform(_transform))
- {
- Drawing.Draw(context);
- }
- }
- }
- }
- }
|