Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom line drawing in WPF
    primarykey
    data
    text
    <p>I'm trying to manually draw a line in WPF by overriding the OnRender method of a control and calling the DrawLine method on the given DrawingContext. I read somewhere that this method call doesn't instantly draw the line, but I can't work out how to actually cause the line to appear.</p> <p>I've tried using a combination of PathGeometry, LineSegments, Line and Polyline controls. I could draw what I wanted then, but offsets weren't quite right (i.e. when drawing a line, it was fine, when drawing a polyline, everything became incorrectly offset).</p> <p>Any advice on this would be great.</p> <p><strong>EDIT</strong></p> <p><em>Pen Code</em></p> <pre><code> private static readonly Pen LinePen = new Pen(new SolidColorBrush(Colors.Green), 3.0d); private static readonly Pen WayPointPen = new Pen(new SolidColorBrush(Colors.Gray), 3.0d); </code></pre> <p><em>Render Code</em></p> <pre><code> protected override void OnRender(DrawingContext drawingContext) { // Draw way points this.DrawWayPoints(drawingContext); if (mDrawing) { // Draw current line this.DrawCurrentLine(drawingContext); } } private void DrawCurrentLine(DrawingContext context) { if(mStartPoint.HasValue &amp;&amp; mEndPoint.HasValue) { // Draw the line context.DrawLine(LinePen, mStartPoint.Value, mEndPoint.Value); } } private void DrawWayPoints(DrawingContext context) { if (mWayPoints.Count &lt; 2) { return; } // Draw all points for (int i = 0; i &lt; mWayPoints.Count - 1; i++) { var start = mWayPoints[i]; var end = mWayPoints[i + 1]; // Draw the line context.DrawLine(WayPointPen, start, end); } } </code></pre> <p><strong>EDIT</strong></p> <p>Test Project: <a href="http://dl.dropbox.com/u/12763956/DrawingTest.zip" rel="nofollow">http://dl.dropbox.com/u/12763956/DrawingTest.zip</a> (Test Project written in Visual Studio 2010)</p> <p>Usage: - Left click within the raised area to add points to the list. - Right click to end drawing and clear points.</p> <p>Note: Custom drawn lines (in OnRender override) do not appear.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload