0

I have b Buttons created dynamically by code and I want to draw a Line between the first node and the last in the same StackPanel. The problem is that whenever I call this function it doesn't make the line from the first to the b Button but instead it move the line to the right.

My code for the Line draw function:

public void CreateALine()
{
    redLine = new Line();

    redLine.X1 = nodul_nou[0].Margin.Left ;
    redLine.Y1 = nodul_nou[b].Margin.Top - 40;

    redLine.X2 = nodul_nou[b].Margin.Left ;
    redLine.Y2 = nodul_nou[b].Margin.Top - 40;

    SolidColorBrush redBrush = new SolidColorBrush();

    redBrush.Color = Colors.Black;
    redLine.StrokeThickness = 4;
    redLine.Stroke = redBrush;

    workplace.Children.Add(redLine);
}

Note: nodul_nou[b] is the Button I am speak about, It does not draw a line between the first Button, nodul_nou[0], and the b Button, nodul_nou[b].

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Vlad Vlad
  • 132
  • 2
  • 13
  • 2
    I think there is a typo: `redLine.Y1 = nodul_nou[b]` should be `redLine.Y1 = nodul_nou[0]`. – LPL Jul 21 '13 at 15:34
  • 2
    1 - a `StackPanel` is not an adequate container for this. You need a `Canvas` or something. 2 - You don't "draw" stuff in WPF by using procedural code like this, you create the proper data and let the UI yourself. 3 - Don't create or manipulate UI elements in code in WPF, the winforms approach is not needed nor desired in WPF. Post a screenshot of what you need and I can tell you the proper way to do it in WPF. – Federico Berasategui Jul 21 '13 at 17:07
  • @HighCore So I should make the line in xaml and then modify it from code ? – Vlad Vlad Jul 21 '13 at 17:37
  • @LPL Both the buttons are on the same point on Y axis, only the X component is different – Vlad Vlad Jul 21 '13 at 17:38
  • 1
    @VladVlad no. You should create a proper ViewModel and have the UI databound to that ViewModel, and probably use an `ItemsControl` to dynamically generate UI elements from the Collections in the ViewModel. Again, post a screenshot of what you need and I can tell you the right way to do it in WPF. – Federico Berasategui Jul 21 '13 at 17:41
  • @HighCore www.imgur.com/VOnKojL As you can see in this picture I the image the rectangles are the buttons also you can consider the lines as button too and I want to draw the red line, but the user can always add new rectangles and lines ( both of them buttons ) and both of them are part of the array nodul_nou[], if the user adds a new button I want to make that line from the first to the new button(the new button will follow the same order). My problem is that when I use the code from my post it does not draw the line to the last button but it just move it to the right. – Vlad Vlad Jul 21 '13 at 17:55
  • 1
    @VladVlad that really looks similar to [my sample](http://stackoverflow.com/a/15821573/643085) of some kind of diagram designer, you don't need any code to do that, you need an `ItemsControl` and a couple of `DataTemplates`. WPF is very different from other frameworks. You can download my sample code and see how it works, so you can understand how this is done in WPF. – Federico Berasategui Jul 21 '13 at 18:31
  • @HighCore Thanks, I'll take a look over your sample. – Vlad Vlad Jul 21 '13 at 18:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33873/discussion-between-vlad-vlad-and-highcore) – Vlad Vlad Jul 21 '13 at 22:12

0 Answers0