Cannot find source for binding with reference原因分析与解决方法 -[WPF开发]

最近在作一个项目展示的WPF应用,增加了一个3D的展示效果后Debug发现都出现Cannot find source for binding with reference这个错误,首先先看xaml文件里的定义。
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"/>

编译过后报错信息如下:

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/

 

posted @ 2011-09-01 13:45  卿之  阅读(2486)  评论(0编辑  收藏  举报
无觅相关文章插件,快速提升流量