-1

我确实有以下代码:

        private static void AddElements(Canvas canvas)
    {
        double canvasHeight = canvas.Height;
        double canvasWidth = canvas.Width;
        double y0 = canvasHeight / 2;
        double x0 = canvasWidth / 2;

        // Defining the new Coordinate-Point (0,0) to mid auf Canvas
        TranslateTransform tt = new TranslateTransform(x0, y0);

        Line line1 = new Line();
        line1.X1 = -350;
        line1.Y1 = 0;
        line1.X2 = 350;
        line1.Y2 = 0;
        line1.Stroke = Brushes.Black;
        line1.StrokeThickness = 2.0;
        line1.RenderTransform = tt;
        canvas.Children.Add(line1);

        Line line2 = new Line();
        line2.X1 = 0;
        line2.Y1 = -350;
        line2.X2 = 0;
        line2.Y2 = 350;
        line2.Stroke = Brushes.Black;
        line2.StrokeThickness = 2.0;
        line2.RenderTransform = tt;
        canvas.Children.Add(line2);


        Label lblN = new Label();
        lblN.Width = 50;

        lblN.Background = Brushes.Red;
        lblN.Margin = new System.Windows.Thickness(0, -350, 0, 0);
        lblN.Content = $"N";
        lblN.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
        lblN.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
        lblN.RenderTransform = tt;
        lblN.Padding = new System.Windows.Thickness(0);
        lblN.BorderBrush = Brushes.Black;
        lblN.BorderThickness = new System.Windows.Thickness(2.0);
        lblN.RenderTransform = tt;
        canvas.Children.Add(lblN);

        Label lblS = new Label();
        lblS.Width = 50;
        lblS.Background = Brushes.Red;
        lblS.Margin = new System.Windows.Thickness(0, 350, 0, 0);
        lblS.Content = $"S";
        lblS.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
        lblS.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
        lblS.RenderTransform = tt;
        lblS.Padding = new System.Windows.Thickness(0);
        lblS.BorderBrush = Brushes.Black;
        lblS.BorderThickness = new System.Windows.Thickness(2.0);
        lblS.RenderTransform = tt;
        canvas.Children.Add(lblS);
    }

此方法在 Menu-Eventhandler 上调用,它在画布中间显示一个坐标系 (0,0)。它应该在顶部显示一个带有“N”的标签,在底部显示一个带有“S”的标签。

但我显示了附加的图像在此处输入图像描述

有谁知道,为什么 lblN 看起来与 lblS 不同?

此致

沃尔哈德

==============

如果我将两个标签对象的高度设置为 15

lblN.Height=15
:
lblS.Height=15

我得到以下信息: 将高度设置为 15 我希望 lblN 在 y 坐标上更高。

4

1 回答 1

0

是什么原因造成的

通过一些测试,我可以肯定地说这lblN.Margin = new System.Windows.Thickness(0, -350, 0, 0);是导致问题的原因。显然,当你给出这样Label的负边距时,它只会向上移动Height,然后它会开始扩展而不是继续移动。所以你最终得到一个Label350 高的。我们可以尝试弄清楚为什么会这样,但实际上,那将失去重点。

诚然,我没有任何直接的文档来支持以下声明,但根据多年的 WPF 经验,我觉得我可以说:

Margin旨在用于在动态布局中为元素之间提供空间,而不是为元素提供绝对位置。

这种行为Label似乎强化了这样一种想法,即Margin以这种方式使用不是设计师计划的事情。

你应该做什么

Canvas有用于给元素设置位置的工具,但您在任何地方都没有使用Canvas's SetLeftSetTopSetRightSetBottom. 看看MSDN 上的例子。您根本不需要使用 aTranslateTransform或 set Margin。相反,您应该计算您希望元素的位置,并使用上面列出的四种方法之一来分配该位置。

额外提示

不要使用canvas.Heightand canvas.Width,使用canvas.ActualHeightandcanvas.ActualWidth代替。第一对仅在您明确设置的大小时才有效Canvas(您似乎是这样)。但是在Canvas动态调整大小的场景中,第一对将是NaN. 第二对总是返回实际大小Canvas

这对您当前的用例没有影响,但以后可能会。如果您根据元素的实际大小(而不是您可能希望的大小)进行计算,请始终使用ActualHeightand ActualWidth

于 2021-03-28T03:09:13.320 回答