0

For some reason, my XAML is not displaying anything for my Items Control. I am using a List of Students as my ItemsSource and referencing the properties of that List to create a custom control.

This is my XAML:

<ItemsControl ItemsSource="{Binding Class.Students}" Grid.Row="1">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <ComboBox>
                                <ComboBox.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="500"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <TextBlock Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"/>
                                            <TextBlock Text="{Binding FavoriteSubject, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1"/>

                                        </Grid>
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>
                            </ComboBox>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

Right now, the drop-down is just empty. The bindings are correct and if I pull them outside the ItemsControl, they display fine. Is there something specific about ItemsControls that I am missing?

  • As your item source, use an ObservableCollection – Kevin Avignon Oct 18 '18 at 16:05
  • Sorry, but why would that matter? –  Oct 18 '18 at 16:09
  • 'The ListBox (and other similar controls) apparently assumes that a bounded property of type IEnumerable never changes. You either have to assign a different collection to the property and raise the NotifyPropertyChanged event, or use the ObservableCollection' ---https://social.msdn.microsoft.com/Forums/vstudio/en-US/c2ca0bf0-0098-49e0-8b66-59c30016d1d1/listboxitemssource-binding-to-ienumerablelttgt?forum=wpf – Kevin Avignon Oct 18 '18 at 16:11
  • Hm, I changed this to an Observable Collection and still have the same issue. –  Oct 18 '18 at 16:28
  • 1
    A ComboBox in the ItemTemplate of an ItemsControl would certainly require a doubly nested collection. However, you haven't bound the ComboBox's ItemsSource. What are you trying to do here? And please show us your view model. – Clemens Oct 18 '18 at 16:49
  • As a note, setting `UpdateSourceTrigger=PropertyChanged` on the Text Binding of a TextBlock is pointless. It has no effect on a OneWay Binding. – Clemens Oct 18 '18 at 16:51
  • If Students is supposed to be a collection of student objects with Name and FavoriteSubject properties, and you want to show them in a Combobox, you should write `` – Clemens Oct 18 '18 at 21:15
  • @Darnold14: You haven't bound the ComboBox to anything. What is displayed in the ItemsControl? – mm8 Oct 19 '18 at 13:26

0 Answers0