我有一個自定義控件,其中包含一個數據網格,我希望能夠將一個ItemsSource綁定到該數據網格,但似乎無法使其工作。我有一個ObservableObjectCollection設置爲依賴項屬性,但當我嘗試運行它時收到錯誤「無法將綁定轉換爲ObservableObjectCollection」。如何將對象的集合綁定到自定義控件
Public Shared ReadOnly ItemsSourceProperty As DependencyProperty = DependencyProperty.Register("ItemsSource", GetType(ObservableObjectCollection), GetType(HomePageControl), New PropertyMetadata(New ObservableObjectCollection()))
Public Property ItemsSourceCollection As ObservableObjectCollection
Get
Return DirectCast(Me.GetValue(HomePageControl.ItemsSourceProperty), ObservableObjectCollection)
End Get
Set(value As ObservableObjectCollection)
Me.SetValue(HomePageControl.ItemsSourceProperty, value)
End Set
End Property
我是否定義了依賴屬性?我看到很多類似於這個問題的答案,他們都說使用依賴屬性。是什麼賦予了?我正在使用ObservableObjectCollection,因爲我不知道什麼類型的對象將被綁定到控件。
請提供設置綁定的代碼/ XAML。如果它是一個真正的自定義控件,請將ControlTemplate放在此處。 –