我在一个xaml表单(MainWindow)上有一个组合框。
我在后面的代码中将Items源设置为ObservableCollection。为了填充组合框,我使用了相对源代码(它位于ItemsControl中),它工作得很好(没有它,如果没有填充):
ItemsSource="{Binding SelectableItems, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"我现在已经将ObservableCollection分解成一个单独的视图模型类,命名为'MainWindowViewModel',组合框不会填充。
我已经将MainWindow的DataContext设置为我的ViewModel,并检查它是否按预期填充了其他控件。
我应该如何构造RelativeSource才能填充组合框?
谢谢
乔
发布于 2011-09-12 19:35:24
我需要在最后添加路径,如下所示:
ItemsSource="{Binding SelectableItems, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.SelectableItems}"发布于 2011-09-12 23:47:01
您不想再使用RelativeSource了。如果不指定RelativeSource (或Source或ElementName),则绑定将根据当前DataContext进行解析。因为DataContext是继承的,所以您的ItemsControl从父Window获取它的DataContext。因此,此绑定将针对您的视图模型进行解析。
ItemsSource="{Binding SelectableItems}"https://stackoverflow.com/questions/7387042
复制相似问题