2015-10-21 49 views
0

我有一个绑定到DataContext的SceneViewModel的ComboBox,但是我想用名为GearViewModel的另一个ViewModel的observableCollection中的数据填充它。 我该怎么做?或者这是可能的。用ComboBox填充不同ViewModel的项目

这里是XAML

<UserControl x:Class="MoviePrepper.View.SceneView" 
DataContext="{Binding SceneViewModel, Source={StaticResource Locator}}"> 

<Grid> 
    <ComboBox ItemsSource="{Binding to observableCollection in GearViewModel}}" SelectedItem="{Binding SceneCollectionView/Equipment, UpdateSourceTrigger=PropertyChanged}"/> 
</Grid> 
</UserControl> 
+0

你是否需要将你的'ComboBox'绑定到两个'ObservableCollection'(一个在'SceneViewModel'中,另一个在'GearViewModel'中)?或者只是将项目绑定到'GearViewModel ObservableCollection'? – ZwoRmi

+0

绑定到GearViewModel中的项ObservableCollection – Phil

+0

所以,如果你想以正确的方式使用MVVM,你需要在你的'SceneViewModel'中有一个'GearViewModel'。 – ZwoRmi

回答

0

可以使用这样的结合实现这一目标:

<ComboBox ItemsSource="{Binding GearViewModel.MyCollection, Source={StaticResource Locator}}" 
      SelectedItem="{Binding Equipment, UpdateSourceTrigger=PropertyChanged}"/> 

ItemsSource属性绑定到GearViewModel.MyCollection财产在你LocatorSelectedItem结合SceneViewModel.Equipment(由UserControlDataContext设置)。

您不清楚在SelectedItem属性上绑定什么属性,所以我做了一些假设。

任何人都应该解决将您的ItemsSource属性绑定到不同视图模型的问题。

+0

非常好,谢谢! – Phil