【问题标题】:Implement Stretch="Fill" for a custom framework element in WPF为 WPF 中的自定义框架元素实现 Stretch="Fill"
【发布时间】:2013-01-28 11:45:18
【问题描述】:

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

我已经成功地绘制了点,但不知道如何实现法线形状(如折线)所具有的 Stretch="Fill" 功能。我的课程代码如下。

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

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];
        }
    }
}

【问题讨论】:

    标签: c# xaml stretch frameworkelement


    【解决方案1】:

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

    【讨论】:

    • 我认为这也会缩放我需要保持不变的标记的大小。我有自己的解决方案,其中涉及使用 DrawingVisuals 和手动处理转换。明天清理干净后我会发布。
    • 你是对的。在 Viewbox 内绘制的任何内容都会被缩放,包括文本、图像、图标等。
    猜你喜欢
    • 2021-08-17
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 2023-03-06
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多