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?
ItemsSource="{Binding Path=DataContext.Options, ElementName=ItemsListBox}"
– 15ee8f99-57ff-4f92-890c-b56153