WPF窗口不显示从Canvas继承的元素

时间:2015-08-21 08:43:17

标签: c# wpf

我希望绘制图形的所有操作都在我的控制之下,继承自Canvas。但窗口不显示MyCanvas。我不知道为什么。

class MyCanvas : Canvas
    {
        private Double XTimeScale;
        private Double YAmpSacle;
        private Double YTopLimit;
        private Double YBotLimit;
        private List<Point> DotsGraph;

public MyCanvas(Double XTimeScale,
                                      Double YAmpSacle,
                                      Double YTopLimit,
                                      Double YBotLimit)
        {
            this.XTimeScale = XTimeScale;
            this.YAmpSacle = YAmpSacle;

            this.YTopLimit = YTopLimit;
            this.YBotLimit = YBotLimit;
            this.Height = (YTopLimit + YBotLimit);
        }

        public List<Line> DrawNet(Double Width, Double Height)
        {
            List<Line> temp = new List<Line>();
            SolidColorBrush brush = new SolidColorBrush();
            brush.Color = Colors.Gray;

            for (int i = 0; i < Width; i+= 10)
            {
                Line Y = new Line();
                Y.Stroke = brush;
                Y.StrokeThickness = 1;
                Y.Y1 = 0;
                Y.Y2 = Height;
                Y.X1 = i;
                Y.X2 = i;
                temp.Add(Y);
            }

            for (int j = 0; j < Height; j += 10)
            {
                Line X = new Line();
                X.Stroke = brush;
                X.StrokeThickness = 1;
                X.X1 = 0;
                X.X2 = Width;
                X.Y1 = j;
                X.Y2 = j;
                temp.Add(X);
            }
            foreach (var t in temp)
                this.Children.Add(t);
            return temp;
        }
}

应该显示MyCanvas的窗口类。我把它添加到Grid:

public partial class ShowCanvas : Window
{
    public ShowCanvas()
    {
         InitializeComponent();
         MyCanvas EAP = new MyCanvas(300, 300, 300, 300);

         Base.Children.Add(EAP); // Base is Grid on window ShowCanvas
    }
}

1 个答案:

答案 0 :(得分:1)

enter image description here

您的窗口确实显示了您的画布,但您的画布没有任何孩子。调用DrawNet方法。

您可以使用名为&#34; Snoop&#34;的开源程序。浏览可视化树并查看视觉效果属性

enter image description here