WPF Położenie

0

Jak w tym przykładzie bindować. Koniecznie dynamicznie ponieważ nie wiem przy starcie ile będzie Lini i Buttonów.
Za każdym razem gdy zmienia położenie Buttona zmienia się również położenie Lini

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication12
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Button s1 = new Button();
        Button s2= new Button();
        Line l1 = new Line();
        public MainWindow()
        {

            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            s1.Height = 50;
            s1.Width = 50;
            s2.Height = 50;
            s2.Width = 50;
            s1.HorizontalAlignment = HorizontalAlignment.Left;
            s1.VerticalAlignment = VerticalAlignment.Top;
            s2.HorizontalAlignment = HorizontalAlignment.Left;
            s2.VerticalAlignment = VerticalAlignment.Top;

            s2.Click += new RoutedEventHandler(s2_Click);
            s1.Click += new RoutedEventHandler(s1_Click);
            s1.Margin = new Thickness(0, 0, 0, 0);
            s2.Margin = new Thickness(200, 200, 0, 0);
            Thickness temp1 = s1.Margin;
            Thickness temp2 = s2.Margin;
            //od button1
            l1.X1 = temp1.Left + 25;
            l1.Y1 = temp1.Top + 25;
            
            //od button2
            l1.X2 = temp2.Left + 25;
            l1.Y2 = temp2.Top + 25;
            SolidColorBrush redBrush = new SolidColorBrush();
            redBrush.Color = Colors.Red;

            // Set Line's width and color
            l1.StrokeThickness = 4;
            l1.Stroke = redBrush;

            grid2.Children.Add(s1);
            grid2.Children.Add(s2);
            grid2.Children.Add(l1);
            
        }

        void s1_Click(object sender, RoutedEventArgs e)
        {
            s1.Margin = new Thickness(50, 50, 0, 0);
            Thickness temp1 = s1.Margin;
            //od button1
            l1.X1 = temp1.Left + 25;
            l1.Y1 = temp1.Top + 25;
        }

        void s2_Click(object sender, RoutedEventArgs e)
        {
            s2.Margin = new Thickness(150, 150, 0, 0);
            Thickness temp1 = s2.Margin;
            //od button
            l1.X2 = temp1.Left + 25;
            l1.Y2 = temp1.Top + 25;
        }
    }
}
0

Nie wiem czy dobrze zrozumiałem, ale textbox gdzie wpisujesz ilość i button który po kliknięci pobiera ta ilość i tworzy w pętli tyle ile potrzeba przycisków i lini nie wystarczy?

0

Nie bo to ma być graf gdzie po wczytaniu macierzy narysuje, z możliwością przemieszczania wierzchołków a wraz z nimi krawędzie

1 użytkowników online, w tym zalogowanych: 0, gości: 1