1
votes

I have a ListBox that displays a List<Item> Items with Item being a custom object. Foreach item I want the user to see a ComboBox with List<string> Options as the Source with the selected Item tying back to a property on a Item . In the list box I have no trouble binding the individual Item properties but how do I reach back up into the DataContext to get my list of options?

View Model is being set as the page's DataContext

class ViewModel
{
      public ObservableCollection<string> Options { get; set; }
      public ObservableCollection<Item> Items { get; set; }
}

Xaml

<ListBox x:Name="ItemsListBox" ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
         <DataTemplate>
             <Grid Height="50" >
                 <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="*"/>
                     <ColumnDefinition Width="*"/> 
                 </Grid.ColumnDefinitions>
                 <TextBlock x:Name="ItemProperty1TB" 
                     Text="{Binding Path=Property1, Mode=OneWay}"
                     Grid.Column="0" 
                 />
                 <ComboBox x:Name="OptionsCB" 
                     SelectedItem ="{Binding ChosenOptions, Mode=TwoWay}" 
                     ItemsSource="{Binding Path=DataContext.Options}"          
                     Grid.Column="1" 
                     PlaceholderText="Option"
                 />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I tried to cut out as much extra code and get to a readable example.

How to bind to a source inside a ListBox different from the ItemsSource already specified This uses AncestorType that does not exist?

ComboBox inside Listbox.ItemTemplate Binding problem This binds to a static resource. Should I put my options into a static resource?

ElementName looks promising but my IDE only recommends Elements scoped to inside the ListBox... DO NOT TRUST VISUAL STUDIO

Am I just going about this all wrong?

2
ViewModel needs to have properties, not fields. Is that the actual code?15ee8f99-57ff-4f92-890c-b56153
No that is not the actual code I will change that quicklyJared Stroebele
Try ItemsSource="{Binding Path=DataContext.Options, ElementName=ItemsListBox}"15ee8f99-57ff-4f92-890c-b56153
@EdPlunkett Thanks It worked!!!!! When I firsted looked at that option the IDE recomended only elements inside that ListBox so I assumed it was Scoped locally. I should not have trusted Visual StudioJared Stroebele
Ha! I was just about to tell you never to trust VS when you updated your comment!15ee8f99-57ff-4f92-890c-b56153

2 Answers

2
votes

Try this:

ItemsSource="{Binding Path=DataContext.Options, ElementName=ItemsListBox}" 
0
votes

You can use RelativeSource property on Combobox binding object to find the parent. Something like this should work

ItemsSource="{Binding Path=DataContext.Options, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"

Replace UserControl with Page if you are using Page or Window for that matter.