1
votes

Using Microsoft Visual studio in a WPF project. I have a ListBox that's ItemsSource is set to a list of objects called LuhFile. LuhFile contains a public string displayName. I want the listbox to display the displayName, however when I set displayname to the ListBox's displayMemberPath, I get this error:

System.Windows.Data Error: 40 : BindingExpression path error: 'displayName' property not found on 'object' ''LuhFile' (HashCode=61470144)'. BindingExpression:Path=displayName; DataItem='LuhFile' (HashCode=61470144); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

c# MainWindow

    private List<LuhFile> luhFileList;

    public MainWindow()
    {
        InitializeComponent();
        luhFileList = new List<LuhFile>();
        luhFileList.Add(new LuhFile("testPath", "testName", "testDisplay"));
        luhListBox.ItemsSource = luhFileList;
    }

c# LuhFile

class LuhFile
{
    public string path;
    public string fileName;
    public string displayName;

    public LuhFile(string givenPath, string givenFileName, string givenDisplayName)
    {
        path = givenPath;
        fileName = givenFileName;
        displayName = givenDisplayName;
    }
}

XAML

<ListBox x:Name="luhListBox" MinHeight="100" MaxHeight="130" DisplayMemberPath="displayName" />
1

1 Answers

3
votes

Data binding in WPF works with public properties, not with fields.

So change your item class to this:

public class LuhFile
{
    public string path { get; set; }
    public string fileName { get; set; }
    public string displayName { get; set; }
    ...
}