Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9047533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:56:03+00:00 2026-06-16T11:56:03+00:00

Im working on flowchart kind of application in asp.net using silverlight.. Im a beginner

  • 0

Im working on flowchart kind of application in asp.net using silverlight.. Im a beginner in Silvelight, Creating the elements (Rectangle,Ellipse,Line.. ) dynamically using SHAPE and LINE Objects in codebehind (c#)

These shapes will be generated dynamically, meaning I’ll be calling a Web service on the backend to determine how many objects/shapes need to be created. Once this is determined, I’ll need to have the objects/shapes connected together.

how to connect dynamically created shapes with a line in Silverlight like a flowchart.

I read the below article, but its not working for me, actualHeight & actualWidth of shapes values are 0.
Connecting two shapes together, Silverlight 2

here is my MainPage.xaml

<UserControl x:Class="LightTest1.MainPage">

<Canvas x:Name="LayoutRoot" Background="White">
    <Canvas x:Name="MyCanvas" Background="Red"></Canvas>
    <Button x:Name="btnPush" Content="AddRectangle" Height="20" Width="80" Margin="12,268,348,12" Click="btnPush_Click"></Button>               
</Canvas>

code behind MainPage.xaml.cs

    StackPanel sp1 = new StackPanel();

    public MainPage()
    {
        InitializeComponent();
        sp1.Orientation = Orientation.Vertical;
        MyCanvas.Children.Add(sp1);
    }

    Rectangle rect1;
    Rectangle rect2;
    Line line1;

    private void btnPush_Click(object sender, RoutedEventArgs e)
    {
        rect1 = new Rectangle()
        {
            Height = 30,
            Width = 30,
            StrokeThickness = 3,
            Stroke = new SolidColorBrush(Colors.Red),
        };
        sp1.Children.Add(rect1);           

        rect2 = new Rectangle()
        {
            Height = 30,
            Width = 30,
            StrokeThickness = 3,
            Stroke = new SolidColorBrush(Colors.Red),
        };
        sp1.Children.Add(rect2);

        connectShapes(rect1, rect2);
    }

    private void connectShapes(Shape s1, Shape s2)
    {
        var transform1 = s1.TransformToVisual(s1.Parent as UIElement);
        var transform2 = s2.TransformToVisual(s2.Parent as UIElement);

        var lineGeometry = new LineGeometry()
          {
              StartPoint = transform1.Transform(new Point(1, s1.ActualHeight / 2.0)),
              EndPoint = transform2.Transform(new Point(s2.ActualWidth, s2.ActualHeight / 2.0))
          }; 

        var path = new Path()
        {
            Data = lineGeometry,
            Stroke = new SolidColorBrush(Colors.Green),
        };
        sp1.Children.Add(path);          
    }

what I am doing in button click event is just adding two rectangle shapes and tring to connect them with a line (like flowchart).
Please suggest what is wrong in my code..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:56:04+00:00Added an answer on June 16, 2026 at 11:56 am

    Try replacing the line

        connectShapes(rect1, rect2);
    

    with

        Dispatcher.BeginInvoke(() => connectShapes(rect1, rect2));
    

    I’m not sure of the exact reason why this works, but I believe the shapes are only rendered once control passes out of your code, and only once they are rendered do the ActualWidth and ActualHeight properties have a useful value. Calling Dispatcher.BeginInvoke calls your code a short time later; in fact, you may notice the lines being drawn slightly after the rectangles.

    The TransformToVisual method behaves in much the same way as the ActualWidth and ActualHeight properties. It will return an identity transformation if the shape hasn’t been rendered. Even if your lines were being drawn with a definite width and height, they would end up being drawn all on top of one another at the top-left.

    I also found that I needed to add the lines to the Canvas, not the StackPanel, in order for them to be drawn over the rectangles. Otherwise the StackPanel quickly filled up with lines with a lot of space above them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working on setting up a SqlDataSource connecting MySQL database to a ASP .NET GridView.
Working through Pro ASP.NET MVC book and I got the following code snippet that
I am working on an asp .net project which I develop a workflow service
Working on a small game using an HTML5 canvas, and javascript. And it's working
Working on a project using Entity Framework (4.3.1.0). I'm trying to figure out how
Working on an app using Struts2, Tomcat 7 and an oracle database. Right now
working on using a formula ive been using for a while to find distances
I'm working on an application for a Nursing students; It is a program where
Working on a web based buying and selling application with PHP MySQL where users
(working in AS2.0 due to player limitations) In my flash application I draw in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.