Set the Itemssource of a Combobox within an Itemtemplate

Asked

Viewed 88 times

1

Hello, I have a Combobox inside the Itemtemplate of a Listbox, the items in that Listbox are defined from an Observablecollection in Viewmodel, and each item in that list is an object with a list(Liston) of options inside that should be shown in Itemssource Combobox, but I cannot define them with Binding.

Follow code for analysis:

<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>

Binding should not work since each Listbox item represents an object in my list?

1 answer

0

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.