Results 1 to 3 of 3

Thread: Drawing straight line

  1. #1

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Drawing straight line

    Hello everybody,

    Is there a way to draw a straight line using available controls? I need to draw dotted straight line between two points having drawing effect, means need to give a feel that the line is moving between two points. I need to draw it dynamically by taking coordinates from the db.

    Thanks.

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Drawing straight line

    Hope I'm reading what you want correctly.

    Code:
    <Line x:Name="myLine" Stroke="Black" Fill="Black" StrokeThickness="2" StrokeDashArray="2,2" />
    Code:
            Dim p1 As New Point(100, 100)
            Dim p2 As New Point(200, 150)
    
            myLine.X1 = p1.X
            myLine.Y1 = p1.Y
    
            Dim sb As New Storyboard
    
            Dim xAnim As New DoubleAnimation
            xAnim.Duration = New Duration(TimeSpan.FromSeconds(2))
            xAnim.From = p1.X
            xAnim.To = p2.X
            Storyboard.SetTarget(xAnim, myLine)
            Storyboard.SetTargetProperty(xAnim, New PropertyPath(Line.X2Property))
            sb.Children.Add(xAnim)
    
            Dim yAnim As New DoubleAnimation
            yAnim.Duration = New Duration(TimeSpan.FromSeconds(2))
            yAnim.From = p1.Y
            yAnim.To = p2.Y
            Storyboard.SetTarget(yAnim, myLine)
            Storyboard.SetTargetProperty(yAnim, New PropertyPath(Line.Y2Property))
            sb.Children.Add(yAnim)
    
            sb.Begin()

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Drawing straight line

    Reread the question and decided you're looking to have the line appear to move rather than animating the drawing of the line.

    Code:
    <Line x:Name="myLine" X1="100" Y1="100" X2="400" Y2="150" Stroke="Black" Fill="Black" StrokeThickness="2" StrokeDashArray="2,2" />
    Code:
            Dim sb As New Storyboard
    
            Dim offsetAnim As New DoubleAnimation
            offsetAnim.Duration = New Duration(TimeSpan.FromSeconds(0.5))
            offsetAnim.From = 4
            offsetAnim.To = 0
            offsetAnim.RepeatBehavior = System.Windows.Media.Animation.RepeatBehavior.Forever
            Storyboard.SetTarget(offsetAnim, myLine)
            Storyboard.SetTargetProperty(offsetAnim, New PropertyPath(Line.StrokeDashOffsetProperty))
            sb.Children.Add(offsetAnim)
    
            sb.Begin()
    Swap the 'From' and 'To' values if you want it to move the other way.
    Last edited by MattP; Nov 17th, 2009 at 06:32 PM. Reason: typo: From duration should be 4 (2 + 2) not 3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width