2011-04-17 47 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