2011-06-01 75 views
1

我有一個usercontrol,它有一個屬性結果。這個用戶控件應該顯示這個ObservableCollection。我認爲用戶控件的XAML代碼並不重要。代碼隱藏看起來像:WPF綁定到用戶控件的屬性

Public Property Results() As ObservableCollection(Of ResultModel) 
    Get 
     Return GetValue(ResultsProperty) 
    End Get 

    Set(ByVal value As ObservableCollection(Of ResultModel)) 
     SetValue(ResultsProperty, value) 
    End Set 
End Property 

Public Shared ReadOnly ResultsProperty As DependencyProperty = _ 
         DependencyProperty.Register("Results", _ 
         GetType(ObservableCollection(Of ResultModel)), GetType(ResourcesGridData), _ 
         New FrameworkPropertyMetadata(Nothing)) 

在我MainView.xaml我有以下XAML代碼傳送到顯示用戶控件:

<controls:ResourcesGridData Results="{Binding Path=ResultsToShow}" /> 

我想將ResultsToShow屬性綁定將MainViewModel的屬性設置爲usercontrol的屬性。

但現在我得到以下錯誤:

System.Windows.Data.BindingExpression ist kein Wert des Typs ASSESS.Data.ResultModel und kann in dieser generischen Auflistung nicht verwendet werden.

我試着翻譯一下:

System.Windows.Data.BindingExpression is not a value of the type ASSESS.Data.ResultModel and can not be used in this generic list.

+1

MainViewModel是此UserControl(ResourcesGridData)的DataContext,還是實際與主窗口/父級控件關聯? – Damascus 2011-06-01 10:34:08

+0

該評論幫助我很多。我不知道,MainView的DataContext自動是usercontrol的DataContext – rakete 2011-06-01 10:43:30

+0

那麼你是否解決了你的問題? :) – Damascus 2011-06-01 10:44:37

回答

0

好了,這是非常簡單的。就我而言,我不需要usercontrol代碼隱藏中的任何屬性。

在我的MainView簡單地寫:

<controls:ResourcesGridData /> 

,並在用戶控件:

ItemsSource="{Binding ResultsToShow}" 

我直接綁定到的MainView的DataContext的財產。

相關問題