2011-04-17 110 views
0

我有一個用戶控件中的網格放置在具有2個集合的窗口中。無法訪問datacontext的屬性

我正在尋找一種方法從我的網格中獲取到集合2。

我已經嘗試了幾件事情:

ItemsSource="{Binding DataContext.Bicycles, RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type CollectionContainer}}}" /> 

<ComboBox Grid.Column="1" Grid.Row="2" ItemsSource="{Binding RelativeSource= 
{RelativeSource FindAncestor, AncestorType=Window, AncestorLevel=1}, 
Path=DataContext.Bicycles}" DisplayMemberPath="Height" /> 

<ComboBox Grid.Column="1" Grid.Row="2" ItemsSource="{Binding RelativeSource= 
{RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Companies}" 
/> 

但每次我的組合框結束了空

回答

0

當您運行應用程序你d查看你的'輸出'窗口,它會告訴你出現的綁定錯誤。所以它會給你一個線索,說明你做錯了什麼。

看起來你不需要前綴DataContext.除非另有說明,子控件的datacontext默認是父級的datacontext。所以如果窗口的DataContext是UserControl的某個ViewModel,並且它的子控件將具有相同的數據上下文。

所以,你應該大概只需要做到這一點:

<ComboBox Grid.Column="1" Grid.Row="2" ItemsSource="{Binding Companies}" /> 
+0

沒有,真正的錯誤是,我放下X:類型窗口,這是不是這樣的,這是因爲使用棱鏡,一切都是一個用戶控制。這和事實我應該已經上升了兩個層次。 – DerMeister 2011-04-18 15:06:02