2016-06-03 4 views
0

Использование Microsoft Visual studio в проекте WPF. У меня есть ListBox, который ItemsSource установлен в список объектов, называемых LuhFile. LuhFile содержит общедоступную строку displayName. Я хочу ListBox для отображения DISPLAYNAME, однако, когда я установил DisplayName к DisplayMemberPath в ListBox, я получаю эту ошибку:Свойство ListBox DisplayMemberPath не найдено

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" /> 

ответ

2

Связывание данных в WPF работает с общедоступными свойствами, а не с полями.

Так изменить класс элементов к этому:

public class LuhFile 
{ 
    public string path { get; set; } 
    public string fileName { get; set; } 
    public string displayName { get; set; } 
    ... 
} 
Смежные вопросы