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 1092583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:42:16+00:00 2026-05-16T23:42:16+00:00

I have two buttons, and need to link them with a line. I bind

  • 0

I have two buttons, and need to link them with a line.
I bind the Line coordinates to the button positions.

I need to add this binding by code.

In XAML this work very well like this:

<Button x:Name="button1" Width="10" Height="10" Canvas.Left="150"  Canvas.Top="150"/>
<Button x:Name="button2" Width="10" Height="10" Canvas.Left="250"  Canvas.Top="100"/>

<Line x:Name="testLine" Stroke="Black"
      X1="{Binding ElementName=button1, Path=(Canvas.Left)}"
      Y1="{Binding ElementName=button1, Path=(Canvas.Top)}"
      X2="{Binding ElementName=button2, Path=(Canvas.Left)}"
      Y2="{Binding ElementName=button2, Path=(Canvas.Top)}"/>

Now, in code, I tried:

    Binding b = new Binding("(Canvas.Left)");
    b.ElementName = "button1";
    testLine.SetBinding(System.Windows.Shapes.Line.X1Property, "(Canvas.Left)");

but this does not work… (

EDIT :

There is one problem when creating dynamically the element:

Button button1 = new Button();
Canvas.SetLeft(button1, 50);
Canvas.SetTop(button1, 50);
button1.Name = "be1";
mainCanvas.Children.Add(button1);

Button button2 = new Button();
Canvas.SetLeft(button2, 150);
Canvas.SetTop(button2, 150);
button2.Name = "be2";
mainCanvas.Children.Add(button2);

Binding b1 = new Binding() { ElementName = "be1", Path = new PropertyPath("(Canvas.Left)") };
Binding b2 = new Binding() { ElementName = "be2", Path = new PropertyPath("(Canvas.Left)") };

testLine.SetBinding(System.Windows.Shapes.Line.X1Property, b1);
testLine.SetBinding(System.Windows.Shapes.Line.X2Property, b2);

It seems that “be1” does not exist or I don’t know what…

button1.RegisterName(button1.Name, button1); //does not help, error...
  • 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-05-16T23:42:18+00:00Added an answer on May 16, 2026 at 11:42 pm

    Are you sure you did not mean:

        Binding b = new Binding("(Canvas.Left)");
        b.ElementName = "button1";
        testLine.SetBinding(System.Windows.Shapes.Line.X1Property, b);
    

    EDIT:

    You shouldnt need the element name at all:

    Button button1 = new Button();
    Canvas.SetLeft(button1, 50);
    Canvas.SetTop(button1, 50);
    mainCanvas.Children.Add(button1);
    
    Button button2 = new Button();
    Canvas.SetLeft(button2, 150);
    Canvas.SetTop(button2, 150);
    mainCanvas.Children.Add(button2);
    
    //juse Source reference not ElementName
    Binding b1 = new Binding() { Source = button1, Path = new PropertyPath("(Canvas.Left)") };
    Binding b2 = new Binding() { Source = button2, Path = new PropertyPath("(Canvas.Left)") };
    
    testLine.SetBinding(System.Windows.Shapes.Line.X1Property, b1);
    testLine.SetBinding(System.Windows.Shapes.Line.X2Property, b2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a cool script that works with two buttons. I need to add
OK, I have two HTML select elements and a button. I need to disable
I have two buttons with the same ID: <button type=submit onclick=//do something id=theID>button 1</button>
I have two buttons for my form - a Save changes button and a
I have two buttons in my first android frame. ok button and cancel button.
In Application i have two buttons in which one button is used as a
I have two div containers with content in them and two buttons outside the
I have two ListBoxes, lstAvailableColors and lstSelectedColors. Between each listbox are two buttons, Add
I have created two link buttons dynamically: for (int i = 0; i <
I have two PHP files that I need to link. How can I link

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.