I'm writing a WPF User Control for my application, wrapping a ListBox and a few other items.
The ListBox has a new ItemTemplate that presents four pieces of information for each item in my list. I can hard code each of the four bindings to specific properties on my list items and they display fine.
However, I want my UserControl to be a bit more flexible.
On ListBox and ComboBox there is a property DisplayMemberPath (inherited from ItemsControl) that seems to "inject" the appropriate property binding into the standard ItemTemplate.
How do I achieve the same result with my user control?
I'd like to set up four new properties to allow configuration of the information displayed:
public string LabelDisplayPath { get; set; }
public string MetricDisplayPath { get; set; }
public string TitleDisplayPath { get; set; }
public string SubtitleDisplayPath { get; set; }
Reviewing ItemsControl.DisplayMemberPath with Reflector seems to go down the rabbit hole, I haven't been able to fathom how it works.
Also, if I'm completely off course - and there's another, more "WPF" technique that I should be using instead, please point me in that direction.
Update
Here's a clarification of what I'm trying to achieve.
The ListBox within my user control displays four pieces of information per item: Label, Title, Subtitle and Metric
In one place, I want to use this User control to display a list of issues. Each issue looks like this:
public class Issue {
public string Code { get; set; }
public string Description { get; set; }
public string Priority { get; set; }
public string Reporter { get; set; }
}
When displaying issues, I want to use the following mappings:
Code --> Label
Description --> Title
Reporter --> Subtitle
Priority --> Metric
Elsewhere in the same application, I have a list of Posts that I want to display using the same UserControl. Each Post looks like this:
public class Post {
public DateTime PostedOn { get; set; }
public string Title { get; set; }
public string Teaser { get; set; }
public int CommentCount { get; set; }
}
When displaying posts, I want to use the following mappings:
PostedOn --> Label
Title --> Title
Teaser --> Subtitle
CommentCount --> Metric
Given that Issues and Posts are quite different abstractions, I don't want to force them to have the same properties, just to allow the UserControl to be used unchanged. Instead, I want to introduce a little configurability so that I can reuse my UserControl in both sites cleanly.