1
<
ec:PathListBox.LayoutPaths
>
< ec:LayoutPath SourceElement =" {Binding ElementName=menu_path1} " Orientation ="OrientToPath" Distribution ="Even" Start ="0.5" />
</ ec:PathListBox.LayoutPaths >
2
3 <!-- 另外一部分,同在一个xaml文件里面 -->
4 < Path x:Name ="menu_path1" Data ="M39.97894,246.6824 C39.97894,246.6824 97.116112,-43.891956 353.81946,62.111926"
Height ="207.838" Margin ="39.979,38.844,0,0" Stretch ="Fill" VerticalAlignment ="Top" Width ="313" HorizontalAlignment ="Left" />
< ec:LayoutPath SourceElement =" {Binding ElementName=menu_path1} " Orientation ="OrientToPath" Distribution ="Even" Start ="0.5" />
</ ec:PathListBox.LayoutPaths >
2
3 <!-- 另外一部分,同在一个xaml文件里面 -->
4 < Path x:Name ="menu_path1" Data ="M39.97894,246.6824 C39.97894,246.6824 97.116112,-43.891956 353.81946,62.111926"
Height ="207.838" Margin ="39.979,38.844,0,0" Stretch ="Fill" VerticalAlignment ="Top" Width ="313" HorizontalAlignment ="Left" />
编译过后报错信息如下:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=menu_path1'. BindingExpression:(no path); DataItem=null; target element is 'LayoutPath' (HashCode=16248163); target property is 'SourceElement' (type 'FrameworkElement')
想了很久找不出这个Binding出了什么问题,上MSDN上查资料,发现这是namescope(查看解释)的问题。也就是说,xaml文件中定义的element都在同一个namescope里面,但是那个 PathListBox却是在ec这个namescope里面,那个LayoutPath里面希望通过binding一个ElementName去获取Path的内容作为LayoutPath的SourceElement。由于那Path元素没有添加到ec这个namescope里的expanded template tree里面,所以就找不到元素menu_path1。
解决的方式有多种,例如可以通过RelativeSource去进行binding而不是ElementName,或者在后台进行binding设置也是可以的。可参考解决讨论:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/feef9913-9371-4377-a621-c14aa998cc6e/