Home > front end >  Combobox ListCollectionView Header not displaying. Groups Items correctly but blank Header
Combobox ListCollectionView Header not displaying. Groups Items correctly but blank Header

Time:06-09

I'm trying to fill a Combobox with a list of class objects. The list items are the class object properties OptionName and they are supposed to be grouped by class object properties OptionCategory. It seems to almost work, the items are grouped correctly and displayed correctly but the Header is just blank. I've checked that the class objects are being correctly generated so they aren't just null/blank properties for OptionCategory. As you can see in the below picture there are two blank headers. The two categories right now are Release and Development and you can see they grouped correctly.

Image of Combobox

XAML

                    <ComboBox Name="ConfigList"  Grid.ColumnSpan="3" HorizontalContentAlignment="Center" VerticalContentAlignment="Top" 
                              SelectedIndex="0" Grid.Column="1" Grid.Row="1" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center" 
                              Width="224" Height="26" SelectionChanged="ConfigList_SelectionChanged">
                        <ComboBox.GroupStyle>
                            <GroupStyle>
                                <GroupStyle.HeaderTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding OptionName}"/>
                                    </DataTemplate>
                                </GroupStyle.HeaderTemplate>
                            </GroupStyle>
                        </ComboBox.GroupStyle>
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding OptionName}"/>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                        <ComboBox.ItemContainerStyle>
                            <Style TargetType="{x:Type ComboBoxItem}">
                                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                            </Style>
                        </ComboBox.ItemContainerStyle>
                    </ComboBox>

CODE BEHIND

        private void PopulateList()
        {
            ConfigList.SelectionChanged -= new SelectionChangedEventHandler(ConfigList_SelectionChanged);

            List<LoadedOption> xmlOptions = xmlFile.AllOptions;
            DataContext = this;
            ListCollectionView lcv = new ListCollectionView(xmlOptions);
            lcv.GroupDescriptions.Add(new PropertyGroupDescription("OptionCategory"));

            ConfigList.ItemsSource = lcv;

            ConfigList.SelectionChanged  = new SelectionChangedEventHandler(ConfigList_SelectionChanged);
        }

CLASS

    public class LoadedOption
    {       
        public Guid OptionID { get; private set; }
        public string OptionName { get; set; }
        public string OptionCategory { get; set; }
        public string SubPathName1 { get; set; }
        public string SubPathName2 { get; set; }
        public string SubPathName3 { get; set; }
        public string DriveLetterSub1 { get; set; }
        public string DriveLetterSub2 { get; set; }
        public string DriveLetterSub3 { get; set; }
        public string DrivePathBase { get; set; }
        public string DrivePathSub1 { get; set; }
        public string DrivePathSub2 { get; set; }
        public string DrivePathSub3 { get; set; }

        public LoadedOption()
        {
            this.OptionID = Guid.NewGuid();
        }
    }

CodePudding user response:

I got it to work by changing Binding OptionName to Binding Name under the GroupStyle.HeaderTemplate section in the XAML. Below is the answer I found.

"The TextBlock Text property is bound to a Name property, but please be aware that this is not the Name property on the data object (in this case the User class). Instead, it is the name of the group, as assigned by WPF, based on the property we use to divide the objects into groups."

  • Related