views:

48

answers:

2

I am new in WPF, need to bind a line to 2 dots.

But the code bellow does not work:

<Canvas>
    <Ellipse x:Name="dot1" Width="5" Height="5" 
             Canvas.Left="50" Canvas.Top="50"/>
    <Ellipse x:Name="dot2" Width="5" Height="5" 
             Canvas.Left="100" Canvas.Top="100"/>

    <Line 
             X1="{Binding ElementName='dot1', Path='Canvas.Left'}" 
             Y1="{Binding ElementName='dot1', Path='Canvas.Top'}"

             X2="{Binding ElementName='dot2', Path='Canvas.Left'}" 
             Y2="{Binding ElementName='dot2', Path='Canvas.Top'}"/>
</Canvas>

Where is the error? Thanks!

+2  A: 

Hello,

I think the error you are getting because of the single quote " ' " , i think it should be like this

<Line 
         X1="{Binding ElementName=dot1, Path=Canvas.Left}" 
         Y1="{Binding ElementName=dot1, Path=Canvas.Top}"

         X2="{Binding ElementName=dot2, Path=Canvas.Left}" 
         Y2="{Binding ElementName=dot2, Path=Canvas.Top}"/>

if it didnt work try this alternative syntax , just a work around : EX:

< TextBox Name="TextBox1">
    < TextBox.Text>
        < Binding ElementName="Slider1" Path="Value" />
    < /TextBox.Text>
< /TextBox>

take a look at this : http://www.wpfdude.com/articles/BindingToElement.aspx

Arrabi
+1  A: 
<Line  Stroke="Black" StrokeThickness="1" 
       X1="{Binding ElementName=dot1, Path=(Canvas.Left)}" 
       Y1="{Binding ElementName=dot1, Path=(Canvas.Top)}"

       X2="{Binding ElementName=dot2, Path=(Canvas.Left)}" 
       Y2="{Binding ElementName=dot2, Path=(Canvas.Top)}"/>
Andy