Draw a Line in WPF by Code

preview_player
Показать описание
small example how to draw a line in WPF

WPF,c#: eine Linie zeichnen in WPF
Views 0 Date 23.06.2015

WPF,c#: eine Linie zeichnen

Aufgabe: Ich möchte eine Linie in einem WPF Formular zeichnen. Die Linie dient zum Prüfen der Pixelwerte.
Hierzu ein kleines Code-Beispiel wie man eine Linie in WPF zeichnet.
Man erstellt ein Line Objekt, stellt die x und y Werte ein und fügt dann die Linie in das Hauptgrid des WPF Formulars zu



Hier der Beispiel-Code

private void btnStart_Click object sender, RoutedEventArgs e

Line objLine = new LineXX;

objLine.Stroke = System.Windows.Media.Brushes.Black; //Umriss
objLine.Fill = System.Windows.Media.Brushes.Black; //Fuellung

objLine.X1 = btnStart.ActualWidth + btnStart.Margin.Left ;
objLine.Y1 = btnStart.ActualHeight +btnStart.Margin.Top;

objLine.X2 = Application.Current.MainWindow.ActualWidth ;
objLine.Y2 = Application.Current.MainWindow.ActualHeight;

MainGrid.Children.Add objLine;

Das XAML Document besteht nur aus dem Grid und dem Start-Button
Рекомендации по теме
Комментарии
Автор

Your video saves me again. Thanks a lot for your tutorials
P.S. Best regards from Lviv, Ukraine

just_kidding_kid
Автор

Super! I come from C++, Java Swing, Java FX, and I'm exploring now WPF with C#. I like computing graphics. Your example is basically ok, but man! I have the same question, i.e. "... basically i don't know why the line doesn't come explicitly at the right (bottom) corner ... but ... // That's really what i need to know, because i have tried with many WPF panels, no matters what panel, coordinate lines coded with C# doesn't recognize (0, 0), nor (Width, Height) from their container panel. Any help / suggestions why? or how to get it on? With XAML they get origin (0, 0) ok. But i develope dynamic graphics at run-time, lines need to be coded.

silmar
Автор

awesome video, but how do i add more lines? and thanks!

adanramirez