如何在WPF中正确刷新自定义形状?

发布于 2024-10-27 10:25:29 字数 2030 浏览 0 评论 0原文

我创建了一条自定义行,旁边有一些文本。该形状是 System.Windows.Shapes.Shape 的子类。由于某种原因,当我更改线的坐标时,文本不会刷新。我了解 InvalidateVisual() 方法,但是每次我移动元素时,我都必须调用它来重绘形状。我确信有更好的方法来做到这一点。我做错了什么? ATM 我没有主意了。

public class MyShape : Shape
{
    LineGeometry line;
    FormattedText text;

    public MyShape()
    {
        line = new LineGeometry();
        text = new FormattedText(
                Edge.Length.ToString(),
                Thread.CurrentThread.CurrentCulture,
                System.Windows.FlowDirection.LeftToRight,
                new Typeface("Verdana"), 10, Brushes.Black);
    }

    // Specify the X1 dependency property:
    public static readonly DependencyProperty X1Property =
        DependencyProperty.Register("X1",
        typeof(double), typeof(MyShape),
        new FrameworkPropertyMetadata(0.0,
        FrameworkPropertyMetadataOptions.AffectsMeasure));

    public double X1
    {
        set { SetValue(X1Property, value); }
        get { return (double)GetValue(X1Property); }
    }

    // Specify the Y1 dependency property:
    public static readonly DependencyProperty Y1Property =
        DependencyProperty.Register("Y1",
        typeof(double), typeof(MyShape),
        new FrameworkPropertyMetadata(0.0,
        FrameworkPropertyMetadataOptions.AffectsMeasure));

    public double Y1
    {
        set { SetValue(Y1Property, value); }
        get { return (double)GetValue(Y1Property); }
    }

    /*Some other Dependency Properties....   and*/

    protected override Geometry DefiningGeometry
    {
        get
        {
            GeometryGroup geometryGroup = new GeometryGroup();
            line.StartPoint = new Point(X1, Y1);
            line.EndPoint = new Point(X2, Y2);
            text.SetFontWeight(FontWeights.ExtraLight);
            Geometry geometry = text.BuildGeometry(new Point((X1 + X2) / 2, (Y1 + Y2) / 2));
            geometryGroup.Children.Add(geometry);
            geometryGroup.Children.Add(line);
            return geometryGroup;
        }
    }
}

I created a custom line with some text next to it. The shape is a subclass of System.Windows.Shapes.Shape. For some reason the text does not refresh when I change the coordinates for the line. I know about the InvalidateVisual() approach, but then every time I move elements around I would have to call it to redraw the shape. I am sure there is a better way of doing it. What am I doing wrong? ATM I am out of ideas.

public class MyShape : Shape
{
    LineGeometry line;
    FormattedText text;

    public MyShape()
    {
        line = new LineGeometry();
        text = new FormattedText(
                Edge.Length.ToString(),
                Thread.CurrentThread.CurrentCulture,
                System.Windows.FlowDirection.LeftToRight,
                new Typeface("Verdana"), 10, Brushes.Black);
    }

    // Specify the X1 dependency property:
    public static readonly DependencyProperty X1Property =
        DependencyProperty.Register("X1",
        typeof(double), typeof(MyShape),
        new FrameworkPropertyMetadata(0.0,
        FrameworkPropertyMetadataOptions.AffectsMeasure));

    public double X1
    {
        set { SetValue(X1Property, value); }
        get { return (double)GetValue(X1Property); }
    }

    // Specify the Y1 dependency property:
    public static readonly DependencyProperty Y1Property =
        DependencyProperty.Register("Y1",
        typeof(double), typeof(MyShape),
        new FrameworkPropertyMetadata(0.0,
        FrameworkPropertyMetadataOptions.AffectsMeasure));

    public double Y1
    {
        set { SetValue(Y1Property, value); }
        get { return (double)GetValue(Y1Property); }
    }

    /*Some other Dependency Properties....   and*/

    protected override Geometry DefiningGeometry
    {
        get
        {
            GeometryGroup geometryGroup = new GeometryGroup();
            line.StartPoint = new Point(X1, Y1);
            line.EndPoint = new Point(X2, Y2);
            text.SetFontWeight(FontWeights.ExtraLight);
            Geometry geometry = text.BuildGeometry(new Point((X1 + X2) / 2, (Y1 + Y2) / 2));
            geometryGroup.Children.Add(geometry);
            geometryGroup.Children.Add(line);
            return geometryGroup;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文