0

我有一個用戶控件,我需要我做了一個簡單的類的列表,稱爲聯繫人:綁定到觀察到的集合,依賴屬性失敗

public class Person { 
    public string Name { get; set; } 
} 
在用戶控件

現在,我需要有一個的ObservableCollection <我可以綁定的人>。所以我想我需要使它成爲一個依賴屬性。所以在用戶控件我有以下幾點:

public static readonly DependencyProperty PersonsDependencyProperty = 
    DependencyProperty.Register("Persons", typeof(ObservableCollection<Person>), 
           typeof(PersonUserControl)); 

,並且屬性是這樣的:

public ObservableCollection<Person> Persons 
{ 
    get 
    { 
     return (ObservableCollection<Person>)GetValue(PersonsDependencyProperty); 
    } 
    set 
    { 
     SetValue(PersonsDependencyProperty, value); 
    } 
} 

現在在我的代碼隱藏MainWindow.xaml我做一個ObservableCollection <人>稱爲PersonList,設置主窗口datacontext自我,並像這樣綁定:

<Local:PersonUserControl Persons="{Binding PersonList}"> 
</Local:PersonUserControl> 

而且我得到的錯誤:異常已被拋出的目標一個調用。 - 沒有進一步的解釋。任何人都可以告訴我如何對此做出反應或者我做錯了什麼。

我希望我已經夠清楚了。

+0

你可以發佈堆棧跟蹤,也是它的內部異常,如果有的話?順便說一句,你應該命名你的財產爲'PersonsProperty',所以它應該是'公共靜態只讀DependencyProperty PersonsDependencyProperty' – nemesv

+0

你可以請顯示你的xaml.cs文件代碼 – ethicallogics

回答

0

PersonsDependencyProperty應該只是PersonsProperty。很難說這是否是沒有更多信息的根本原因,但這確實是一個問題。 WPF將「屬性」附加到綁定路徑中以查找相關的依賴項屬性。因此,它不會找到你的。