简体   繁体   中英

fill ComboBox with items from different ViewModel

I have a ComboBox binded to DataContext SceneViewModel, but I want to fill it with data from an observableCollection from another ViewModel called GearViewModel. How do I do this? or is this possible.

Here is the 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>

You can achieve this using a binding like this:

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

Where the ItemsSource property binds to the GearViewModel.MyCollection property in your Locator , and the SelectedItem binds to the SceneViewModel.Equipment (as set by the DataContext of the UserControl ).

It is not clear exactly what property you had in mind for binding on the SelectedItem property, so I made some assumptions.

Anywho, that should solve the problem with binding your ItemsSource property to a different view model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM