1

I am trying to bind a ComboBox in WPF to a list of LogLevels. Class below

public class LogLevel
{
    public string Label;
    public byte Val;

    public static List<LogLevel> GetAll()
    {
        return new List<LogLevel> {
            new LogLevel{
                 Label = "None",
                  Val = 0
            },
            new LogLevel{
                 Label = "Info",
                  Val = 3
            },
            new LogLevel{
                 Label = "Error",
                  Val = 2
            }
        };
    }
}

I want to be able to populate a ComboBox with the result of GetAll and set the DisplayMemberPath to "Label"

ddlLogLevel.ItemsSource = LogLevel.GetAll();

<ComboBox DisplayMemberPath="Label" x:Name="ddlLogLevel"  />

I have tried to set the values statically in the code-behind of the control as well, but the combobox has blank labels. The selecteditem and index is being populated as expected, just not sure how to get the Display value to work

Isaac Levin
  • 2,809
  • 9
  • 49
  • 88

1 Answers1

2

DisplayMemberPath type is property, not field.

If you change this, you can see combobox items.

public class LogLevel
{
    public string Label { get; private set; }
    public byte Val { get; private set; }

    public static List<LogLevel> GetAll()
    {
        return new List<LogLevel> {
            new LogLevel{
                 Label = "None",
                  Val = 0
            },
            new LogLevel{
                 Label = "Info",
                  Val = 3
            },
            new LogLevel{
                 Label = "Error",
                  Val = 2
            }
        };
    }
}
level120
  • 46
  • 3