Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an two objects of the same type, called Object1 and Object2.

Inside these object are two collections - Collection1 and Collection2.

I have a listview with ItemsSource="{Binding Path=Object1.Collection1}".

The ListView contains a column with a ComboBox with ItemsSource="{Binding Path=Object2.Collection2}".

When I run the app, the combobox is always empty (no items).

How do I bind a combobox to a different items source in a listview?

What I have tried:

I've tried setting the datacontext on the combo box to {Binding DataContext.Object2}, but that had no effect.

When I drop down the combo, DataContext and ItemsSource are both null.
Posted
Updated 17-Sep-19 7:08am

If Object1 and Object2 are defined in the "window" class, then

this.DataContext = this;

in the constructor should do it (for the case where Path=Object1...)
 
Share this answer
 
Comments
#realJSOP 17-Sep-19 11:13am    
Yeah, I thought so too, but no luck. I think I'm gonna haveto do some distasteful data re-organization... :/
[no name] 17-Sep-19 11:28am    
Had a hard time picturing combo boxes in a list view. Now "user controls" with combos in a listview is another matter.

The list view item source is one collection.

The user controls bind to items in the collection.

The combos bind to the collections in the item that the user control is bound to.

Using Observable collections may or may not be needed; depending on whether or not you are updating the collections on the fly.

Easy. ;o)

("Templates" serve the same purpose as user controls that you create yourself and load into a list view - I prefer UC's as soon as you get "fancy").
#realJSOP 17-Sep-19 11:51am    
I did some ugly data re-org, and got it to work... I thought I'd be able to specify multiple data contexts, but nothing I tried worked.
[no name] 17-Sep-19 12:18pm    
You can; you just have to be aware of how they get resolved.

As soon as you explicitly set a data context "in the middle" of a visual tree, everything "below that" will resolve to that. Everything above, resolves to any data context "above". But only if "their" Path makes sense for that data context.
If I've understood your structure correctly, something like this should work:
XML
<Window ...>
    <Window.Resources>
        <CollectionViewSource x:Key="Collection2View" Source="{Binding Path=Object2.Collection2}" />
    </Window.Resources>
    
    <ListView ItemsSource="{Binding Object1.Collection1}" ...>
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="Collection 2">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{StaticResource Collection2View}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900