Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 3af440e

Browse files
committed
Added LinkDropDown.DisplayMemberPath support.
A bit hackishly, but I couldn't find a better way.
1 parent 827b5b7 commit 3af440e

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/GitHub.UI/Controls/ComboBoxes/LinkDropDown.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ public object Header
5454
/// <summary>
5555
/// Gets the text to display in the link.
5656
/// </summary>
57-
public string LinkText => (string)GetValue(LinkTextProperty);
57+
public string LinkText
58+
{
59+
get { return (string)GetValue(LinkTextProperty); }
60+
private set { SetValue(LinkTextPropertyKey, value); }
61+
}
5862

5963
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
6064
{
@@ -69,7 +73,33 @@ private static void HeaderChanged(DependencyObject d, DependencyPropertyChangedE
6973

7074
private void UpdateLinkText()
7175
{
72-
SetValue(LinkTextPropertyKey, SelectedItem?.ToString() ?? Header?.ToString());
76+
if (SelectedItem != null)
77+
{
78+
var item = SelectedItem;
79+
80+
// HACK: The correct way to do this is to use a ContentPresenter in the control
81+
// template to display the link text and do a:
82+
//
83+
// ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
84+
//
85+
// to correctly display the DisplayMemberPath. However I couldn't work out how
86+
// to do it like this and get the link text looking right. This is a hack that
87+
// will work as long as DisplayMemberPath is just a property name, which is
88+
// all we need right now.
89+
if (string.IsNullOrWhiteSpace(DisplayMemberPath))
90+
{
91+
LinkText = item.ToString();
92+
}
93+
else
94+
{
95+
var property = item.GetType().GetProperty(DisplayMemberPath);
96+
LinkText = property?.GetValue(item)?.ToString();
97+
}
98+
}
99+
else
100+
{
101+
LinkText = Header.ToString();
102+
}
73103
}
74104
}
75105
}

0 commit comments

Comments
 (0)