[英]Drawing Line Arrays WPF
I'm trying to move my project from WinForms to WPF, but i couldn't succeed in this. 我试图将我的项目从WinForms移到WPF,但我无法成功。 I had a picture box, with a full of lines one after another and i could just use DrawLine(pen,point,point) in a loop.
我有一个图片框,一个接一个的行,我可以在循环中使用DrawLine(pen,point,point)。 But when i tried to do this by using WPF, i couldn't menage.
但是,当我尝试使用WPF进行此操作时,我无法进行管理。
This is the code snippet that i've used in WinForms : 这是我在WinForms中使用的代码片段:
for (x = 0; x < X_COORD_END - X_COORD_START; x += 1)
{
System.Drawing.Point point1 = new System.Drawing.Point(x, 30);
System.Drawing.Point point2 = new System.Drawing.Point(x, 60);
e.Graphics.DrawLine(myPen, point1, point2);
}
And the result was : 结果是:
I've tried to use Line array on WPF, but i gave an error on canvas.Children.Add line. 我试图在WPF上使用Line数组,但是在canvas.Children.Add行上给出了错误。 Now i'm trying to use collection of points, but i can't arrange points as i want to.
现在,我正在尝试使用积分的集合,但是我无法按照自己的意愿排列积分。 Here is what i've tried :
这是我尝试过的:
private void DrawLine(Point[] points)
{
Polyline line = new Polyline();
PointCollection collection = new PointCollection();
foreach (Point p in points)
{
collection.Add(p);
}
line.Points = collection;
line.Stroke = new SolidColorBrush(Colors.Black);
line.StrokeThickness = 20;
scanCanvas.Children.Add(line);
}
for (int counter = 0; counter < 1000; counter++ )
{
points[counter] = new Point(counter, 30);
}
DrawLine(points);
Use Stackpanel for your scenario. 针对您的方案使用Stackpanel。 I have used a line array, which is similar to the thing which you have done in winforms.
我使用了一个行数组,它类似于您在winforms中所做的事情。
MainWindow.xaml MainWindow.xaml
<Window x:Class="SO.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel x:Name="stackGraphicsArea" Orientation="Horizontal"/>
</Window>
MainWindow.xaml.cs MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Line[] lines = new Line[1000];
for(int i=0; i<1000;i++)
{
lines[i] = new Line();
lines[i].Stroke = new SolidColorBrush(Colors.Red);
lines[i].StrokeThickness = 5;
lines[i].X1 = 0;
lines[i].X2 = 0;
lines[i].Y1 = 30;
lines[i].Y2 = 20;
}
DrawLines(lines);
}
private void DrawLines(Line[] _lines)
{
foreach (Line _line in _lines)
{
stackGraphicsArea.Children.Add(_line);
}
}
}
Sriman Reddy has answered my question already but i think i should post my own solution too : 斯里曼·雷迪(Sriman Reddy)已经回答了我的问题,但我想我也应该发布自己的解决方案:
private void DrawLine(int x_coord_start, int y_coord_start, int x_coord_end, int thickness, Color color)
{
Random rand = new Random();
for (int x = 0; x < x_coord_end - x_coord_start; x++)
{
double newTop = rand.Next();
Line top = new Line();
top.Stroke = new SolidColorBrush(color);
top.StrokeThickness = x + 1;
top.X1 = x_coord_start + x;
top.Y1 = y_coord_start;
top.X2 = x_coord_start + x;
top.Y2 = y_coord_start + thickness;
Canvas.SetTop(top, 0);
Canvas.SetLeft(top, 0 /*x * top.Width*/);
scanCanvas.Children.Add(top);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.