Set the ItemsSource of a ComboBox within an ItemTemplate

1
Hello, I have a ComboBox inside the ItemTemplate of a ListBox, the items in this ListBox are defined from an ObservableCollection in the ViewModel, and each item in that list is an object with a list ( List ). of options inside that should be shown in the ItemsSource of the ComboBox, but I can not set them with Binding.

Here is code for review:

<DataTemplate>
                <Grid>
                    <StackPanel Orientation="Horizontal">
                        <gi:GIComboBox Width="100" Height="20"  ItemsSource="{Binding ListaDe}" />
                        <gi:GILabelSubtitulo Content="----->" />
                        <gi:GIInputText Height="20" Width="100" Margin="10 0 0 0" Mask=""/>
                        <gi:GIBotaoComum Height="20" Width="30" Content="..." Margin="10 0 0 0" />
                    </StackPanel>
                </Grid>

Should not Binding work since each ListBox item represents an object in my list?

    
asked by anonymous 29.04.2015 / 16:27

1 answer

0

Yes, you're right. The DataContext of DataTemplate is the data itself that is being represented, that is, each item in the list. To make the binding work properly, you must make explicit the location that it should look. For example, you can tell him to look for the first ancestor of type ListBox and see the value of DataContext.ListaDe of it (assuming DataContext of it is ViewModel).

ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListBox}, Path=DataContext.ListaDe}"/>
    
18.08.2017 / 02:35