Monday, December 31, 2012

Connecting two dynamically created shapes using line shape in silverlight

Connecting two dynamically created shapes using line shape in silverlight

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..

Answers & Comments...

Answer: 1

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.

by : Luke Woodwardhttp://stackoverflow.com/users/48503




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog