2010-01-19 67 views
1

设置一个DataContext的用户控件我有哪里我试图设置DataContext如下用户控件:错误而在WPF

<UserControl.DataContext> 
    <Binding ElementName="dataGrid" Path="MyViewModel"> 

    </Binding> 
</UserControl.DataContext> 

这里dataGrid是孩子DataGrid控制和MyViewModel是我ViewModel类。目前其给予以下错误,同时运行时:

Cannot find source for binding with reference 'ElementName=dataGrid'. BindingExpression:Path=MyViewModel; DataItem=null; target element is 'UserControl1' (Name=''); target property is 'DataContext' (type 'Object')

任何人都可以请帮助这里有什么问题吗?

+0

您可以在此StackOverflow答案中找到答案: http://stackoverflow.com/a/9122644/2342414 – benshabatnoam 2015-09-03 13:03:29

回答

0

在DataGrid的DataContext上设置MyViewModel吗?

如果路径的DataContext所以更改MyViewModel,你是好去... ... 如果没有,请将您的MyViewModel类的DataContext从绑定移除的ElementName,它应该工作为好;)

+0

错误指出绑定找不到属性的“源”,而不是属性本身。虽然我相信下一步它不会找到属性:) – Snowbear 2011-03-20 11:18:46

0

这种结合试图访问dataGrid.MyViewModel,但MyViewModel不是DataGrid的属性...你应该做这样的事情,而不是:

<Binding ElementName="dataGrid" Path="DataContext.MyViewModel"> 
1

的问题是最有可能是由于名称作用域的限制。元素名称绑定仅在定义的边界内正常工作。这个特定的错误是说它找不到指定的元素“dataGrid”。你能展示更多的周围XAML吗?