Search code examples
c#wpfdata-bindingcombobox

C# WPF Combobox DIsplayMemberPath not displaying


Seem to be having a problem where I'm trying to set a date property of an object to display within a combo-box selection, the code runs without complaining and when stepping through the code it seems to set the property but when the application is built the selection options display blank rows.

C# Code:

                List<Item> items = new List<Item>();

                Item item = new Item(itemDate ,incidentDate, incidentSeverity, incidentDescription);

                items.Add(item);

                foreach (Item x in items)
                {
                    SelectItemComboBox.Items.Add(x);
                    SelectItemComboBox.DisplayMemberPath = x.ItemDate.ToShortDateString();
                }
            }

XAML:

<ComboBox Name="SelectItemComboBox" HorizontalAlignment="Left" Height="30" VerticalAlignment="Top" Width="150"/>

I was thinking that I might require doing some sort of data binding from the XAML code, but I'm not sure why I would need to do this when if I removed the DisplayMemberPath code it would display the object detail, just not the underlying value, therefore I'm not sure why it's no longer displaying anything with that additional line that seems to do as expected from the debugging perspective.

Any advice would be much appreciated, thanks for your time.


Solution

  • List<Item> items= new List<Item>();
    
    Item item = new Item(itemDate,incidentDate,incidentSeverity, incidentDescription);
    
    items.Add(item);
    
    SelectItemComboBox.ItemsSource = items;
    
    // You need to set the NAME of the property from Item class.
    // For example, "ItemDate":
    SelectItemComboBox.DisplayMemberPath = "itemDate";