繁体   English   中英

在代码中绘制多个网格的最简洁方法-C#WPF

[英]Most concise way to draw multiple grids in code - C# WPF

我需要使用C#WPF绘制用户定义的网格数量。 我的代码可以工作,但是如果允许的最大网格数很大,则会太长-是否有更简洁的方法?

下面的代码显示了当前如何在gridCanvas(WPF网格控件)上绘制1个网格-对于可能需要的每个其他网格重复进行此操作。

private void DrawInvGrid(int[,] initialPosn)
{          
    PathFigure myPathFigure1 = new PathFigure();
    GeometryGroup myGeometryGroup1 = new GeometryGroup();
    LineSegment myLineSegment1 = new LineSegment();      
    PathSegmentCollection myPathSegmentCollection1 = new PathSegmentCollection();
    PathFigureCollection myPathFigureCollection1 = new PathFigureCollection();

    // repeat declarations for each grid

    int gridx = 5;
    int gridy = 5;
    int gridsize = 11;

    // create grid 1

    for (int z = 0; z < 5; z++)
    {
        int startpt_1x = (initialPosn[0, 0] - 5);
        int startpt_1y = (initialPosn[0, 1] - 5);
        for (int i = 0; i <= gridx; i++)
        {
            myPathFigure1 = new PathFigure();
            myPathSegmentCollection1 = new PathSegmentCollection();
            myPathFigure1.StartPoint = new System.Windows.Point(startpt_1x + 
                                                         (i * gridsize), startpt_1y);

            myLineSegment1 = new LineSegment();
            myLineSegment1.Point = new System.Windows.Point(startpt_1x + (i * gridsize),
                                                startpt_1y + gridy * gridsize);
            myPathSegmentCollection1.Add(myLineSegment1);
            myPathFigure1.Segments = myPathSegmentCollection1;
            myPathFigureCollection1.Add(myPathFigure1);

            xgridpos1.Add(startpt_1x + (i * gridsize));
         }

         for (int i = 0; i <= gridy; i++)
         {
             myPathFigure1 = new PathFigure();
             myPathSegmentCollection1 = new PathSegmentCollection();
             myPathFigure1.StartPoint = new System.Windows.Point(startpt_1x, startpt_1y +
                                                    (i * gridsize));
             myLineSegment1 = new LineSegment();
             myLineSegment1.Point = new System.Windows.Point(startpt_1x + gridx * 
                          ridsize, startpt_1y + (i * gridsize));
             myPathSegmentCollection1.Add(myLineSegment1);
             myPathFigure1.Segments = myPathSegmentCollection1;
             myPathFigureCollection1.Add(myPathFigure1);

             ygridpos1.Add(startpt_1y + (i * gridsize));
         }
    }  

    // repeat grid creation loop for each grid

    // display grid 1
    gridCanvas.Children.Clear();
    PathGeometry myPathGeometry1 = new PathGeometry();
    myPathGeometry1.Figures = myPathFigureCollection1;

    myPath1.Stroke = System.Windows.Media.Brushes.Green;
    myPath1.StrokeThickness = 1;
    myPath1.ToolTip = gridname;
    myPath1.Data = myPathGeometry1;

    gridCanvas.Children.Add(myPath1);

    // repeat display routine for each grid
}

所有网格在任何特定运行中都是相同的-只有网格的位置(由int[,] initialPosn定义)在网格之间变化。 任何有关更有效的替代方案的建议表示赞赏。

将重复代码提取到一个方法中,该方法仅将那些更改的值(在您的情况下为位置)作为参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM