How to implement DisplayMemberPath for my Wpf UserControl?

I am writing a WPF control for my application, wrapping a ListBox and several other elements.

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 in my list items, and they display normally.

However, I want my UserControl to be a little more flexible.

In ListBox and ComboBox there is a DisplayMemberPath property (inherited from ItemsControl), which seems to โ€œinjectโ€ the corresponding property binding into a standard ItemTemplate.

How to achieve the same result using my user control?

I would like to configure four new properties to enable the display of information:

public string LabelDisplayPath { get; set; } 
public string MetricDisplayPath { get; set; }
public string TitleDisplayPath { get; set; }
public string SubtitleDisplayPath { get; set; }

Overview of ItemsControl.DisplayMemberPath with a reflector, it seems to go down the rabbit hole, I could not understand how it works.

Also, if I'm completely unaware - and there is another, more "WPF" method that I should use instead, point me in that direction.

Update

Here is an explanation of what I'm trying to achieve.

The ListBox inside my user control displays four pieces of information for each item: Label, Title, Subtitles and Metric

In one place, I want to use this control to display a list of problems. Each problem is as follows:

public class Issue {
    public string Code { get; set; }
    public string Description { get; set; }
    public string Priority { get; set; }
    public string Reporter { get; set; }
}

:

Code --> Label
Description --> Title
Reporter --> Subtitle
Priority --> Metric

, , UserControl. :

public class Post {
    public DateTime PostedOn { get; set; }
    public string Title { get; set; }
    public string Teaser { get; set; }
    public int CommentCount { get; set; }
}

:

PostedOn --> Label
Title --> Title
Teaser --> Subtitle
CommentCount --> Metric

, , , UserControl . , UserControl .

+5
4

- , ...

ItemsControl, DisplayMemberPath, DisplayMemberTemplateSelector ItemTemplateSelector. , DataTemplate DisplayMemberPath. , DisplayMemberPath.

Usercontrol, " ", , , ItemsControl.

+2

ViewModels , : () ItemViewModelBase , Label, Title, Subtitle Metric. , , , , . - . , DataTemplate ItemViewModelBase, .

:

public abstract class ItemViewModelBase
{
    public abstract string Label { get; }
    public abstract string Title { get; }
    public abstract string Subtitle { get; }
    public abstract string Metric { get; }
}

public class IssueViewModel : ItemViewModelBase
{
    private Issue _issue;

    public override string Label { get { return _issue.Code; } }
    public override string Title { get { return _issue.Description; } }
    public override string Subtitle { get { return _issue.Reporter; } }
    public override string Metric { get { return _issue.Priority; } }

    public IssueViewModel(Issue issue)
    {
        _issue = issue;
    }
}

public class PostViewModel : ItemViewModelBase
{
    private Post _post;

    public override string Label { get { return _post.PostedOn.ToString(); } }
    public override string Title { get { return _post.Title; } }
    public override string Subtitle { get { return _post.Teaser; } }
    public override string Metric { get { return _post.CommentCount.ToString(); } }

    public PostViewModel(Issue post)
    {
        _post= post;
    }
}

ListBox ItemViewModelBase, Issue Post, DataTemplate, :

<DataTemplate DataType="{x:Type local:ItemViewModelBase}">
    <StackPanel>
        <TextBlock x:Name="txtLabel" Text="{Binding Label}"/>
        <TextBlock x:Name="txtTitle" Text="{Binding Title}"/>
        <TextBlock x:Name="txtSubtitle" Text="{Binding Subtitle}"/>
        <TextBlock x:Name="txtMetric" Text="{Binding Metric}"/>
    </StackPanel>
</DataTemplate>

, DataTemplate -, , . , - , DateTime int. , : DateTime.ToString(), PostViewModel.Label - .

+1

, , , dependencyProperty

XAML:

<UserControl x:Name="myControl">
    <StackPanel>
        <TextBlock x:Name="textBlock" 
                   Text="{Binding ElementName=myControl, Path=LabelDisplayPath}" />
        <TextBlock x:Name="textBlock" 
                   Text="{Binding ElementName=myControl, Path=MetricDisplayPath}" />
        <TextBlock x:Name="textBlock" 
                   Text="{Binding ElementName=myControl, Path=TitleDisplayPath}" />
        <TextBlock x:Name="textBlock" 
                   Text="{Binding ElementName=myControl, Path=SubtitleDisplayPath}" />
    </StackPanel>
</UserControl>

DisplayPaths -

UserControl : LabelDisplayPathProperty, MetricDisplayPathProperty...

DisplayMemberPath :

<ListView ItemsSource="{Binding IssueList}"> 
    <ListView.ItemTemplate>
        <DataTemplate>
            <myUC:Control1 LabelDisplayPath = "{Binding Path=Issue.Code}"
                           MetricDisplayPath = "{Binding Path=Issue.Priority}"
                           TitleDisplayPath = "{Binding Path=Issue.Description}"
                           SubtitleDisplayPath = "{Binding Path=Issue.Reporter}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
0

, .
, ( , x: Key x: Name ). , . / , :)

0

All Articles