2010-01-19 33 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嗎?