1

我有一个自定义框架元素,它是折线的对偶。此元素旨在在折线中的点位置绘制标记,并将充当叠加层。

我已经成功地绘制了这些点,但是对于如何实现诸如折线之类的法线形状所具有的 Stretch="Fill" 功能感到茫然。我的课程代码如下。

如果有人能告诉我如何为此添加拉伸功能,我将不胜感激。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ReactiveUI;

namespace Weingartner.WPF
{
    public class PolyMarkers : FrameworkElement
    {
        static PolyMarkers()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PolyMarkers), new FrameworkPropertyMetadata(typeof(PolyMarkers)));
        }

        public PointCollection Points
        {
            get { return (PointCollection)GetValue(PointsProperty); }
            set { SetValue(PointsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Points.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PointsProperty =
            DependencyProperty.Register(
                "Points", 
                typeof(PointCollection), 
                typeof(PolyMarkers), 
                new PropertyMetadata(new PointCollection()));

        private VisualCollection Children;

        public PolyMarkers()
        {

            this.WhenAny(t => t.Points, x =>
            {
                return x.Value.Select(p =>
                {
                    return CreateDrawingVisualEllipses(p, 10, 10);
                });

            }).Subscribe(x =>
            {
                foreach (var shape in x)
                {
                    AddVisualChild(shape);
                }
                InvalidateMeasure();
                InvalidateArrange();
                InvalidateVisual();
            });


        }

        private DrawingVisual CreateDrawingVisualEllipses(Point location, int dx, int dy)
        {
            DrawingVisual drawingVisual = new DrawingVisual();
            DrawingContext drawingContext = drawingVisual.RenderOpen();

            drawingContext.DrawEllipse(Brushes.Maroon, null, location, dx, dy);
            drawingContext.Close();

            return drawingVisual;
        }


        // Provide a required override for the VisualChildrenCount property.
        protected override int VisualChildrenCount
        {
            get { return Children.Count; }
        }

        // Provide a required override for the GetVisualChild method.
        protected override Visual GetVisualChild(int index)
        {
            if (index < 0 || index >= Children.Count)
            {
                throw new ArgumentOutOfRangeException();
            }

            return Children[index];
        }
    }
}
4

1 回答 1

2

最简单的做法是将 a 包裹Viewbox在整个元素的内容上,让它为您处理缩放。如果您想在控件内的较低级别执行此操作,您将需要覆盖所有 Measure 和 Arrange 方法并手动进行所有缩放,这可能是很多(非常容易出错)的工作。

于 2013-01-28T18:18:41.820 回答