WPF中的绑定问题

时间:2010-09-20 11:59:10

标签: .net wpf binding

我是WPF的新手,需要将一行绑定到2个点。

但是下面的代码不起作用:

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

错误在哪里?谢谢!

2 个答案:

答案 0 :(得分:3)

我认为你得到的错误是因为单引号“'”,我认为它应该是这样的          

<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}"/>

如果它不起作用尝试这种替代语法,只需解决一下: EX:

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

看看这个: http://www.wpfdude.com/articles/BindingToElement.aspx

答案 1 :(得分:1)

<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)}"/>