Reputation: 16710
I have a few DTOs that I am using which are designed like this:
ProjectPrimitives
string Name
int ProjectId
ProjectDetails : ProjectPrimitives
string ReportName
string Description
// Others
Project
ProjectDetails details
List<Contact> contacts
// Some more
In my application I have a listbox where I would like to display Project
objects. Previously, I was displaying ProjectPrimitives
by doing the following:
projectListBox.DisplayMember = "Name";
projectListBox.ValueMember = "ProjectId";
projectListBox.DataSource = projects; // This is a List<ProjectPrimitives>
However, now that I'm displaying Project
objects, I can't get this to work. I've tried:
projectListBox.DisplayMember = "Details.Name";
projectListBox.ValueMember = "Details.ProjectId";
projectListBox.DataSource = projects; // This is now a List<Project>
But that didn't work. All it does is display the object (MyProject.DTOs.Project
). What can I do to display the name?
EDIT
The ProjectPrimitives class:
public class ProjectPrimitives
{
public int ProjectId {get; set;}
public string Name {get; set;}
}
The Project class:
public class Project
{
public ProjectDetails details = new ProjectDetails();
}
Upvotes: 1
Views: 5590
Reputation: 81635
They have to be Properties, not Fields:
Public string Name {get; set; }
Public int ProjectId { get; set; }
I don't think the DisplayMember property can handle "sub-properties", so you would have to create these properties in your Project class:
public class Project {
public ProjectDetails details = new ProjectDetails();
public string Name {
get { return details.Name; }
}
public int ProjectId {
get { return details.ProjectId; }
}
}
WinForms does not have the same breadth of data binding capabilities as WPF. This is probably one of the those limitations.
Upvotes: 3