|
| 1 | +using System.Windows; |
| 2 | +using System.Windows.Controls; |
| 3 | + |
| 4 | +namespace GitHub.UI |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// A ComboBox that displays as a link with a dropdown. |
| 8 | + /// </summary> |
| 9 | + public class LinkDropDown : ComboBox |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Defines the <see cref="LinkText"/> property. |
| 13 | + /// </summary> |
| 14 | + static readonly DependencyPropertyKey LinkTextPropertyKey = |
| 15 | + DependencyProperty.RegisterReadOnly( |
| 16 | + "LinkText", |
| 17 | + typeof(string), |
| 18 | + typeof(LinkDropDown), |
| 19 | + new FrameworkPropertyMetadata(string.Empty)); |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Defines the <see cref="Header"/> property. |
| 23 | + /// </summary> |
| 24 | + public static readonly DependencyProperty HeaderProperty = |
| 25 | + HeaderedItemsControl.HeaderProperty.AddOwner( |
| 26 | + typeof(LinkDropDown), |
| 27 | + new FrameworkPropertyMetadata(typeof(LinkDropDown), HeaderChanged)); |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Defines the readonly <see cref="LinkText"/> property. |
| 31 | + /// </summary> |
| 32 | + public static readonly DependencyProperty LinkTextProperty = |
| 33 | + LinkTextPropertyKey.DependencyProperty; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Initializes static members of the <see cref="LinkDropDown"/> class. |
| 37 | + /// </summary> |
| 38 | + static LinkDropDown() |
| 39 | + { |
| 40 | + DefaultStyleKeyProperty.OverrideMetadata( |
| 41 | + typeof(LinkDropDown), |
| 42 | + new FrameworkPropertyMetadata(typeof(LinkDropDown))); |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Gets or sets a header to use as the link text when no item is selected. |
| 47 | + /// </summary> |
| 48 | + public object Header |
| 49 | + { |
| 50 | + get { return GetValue(HeaderProperty); } |
| 51 | + set { SetValue(HeaderProperty, value); } |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Gets the text to display in the link. |
| 56 | + /// </summary> |
| 57 | + public string LinkText |
| 58 | + { |
| 59 | + get { return (string)GetValue(LinkTextProperty); } |
| 60 | + private set { SetValue(LinkTextPropertyKey, value); } |
| 61 | + } |
| 62 | + |
| 63 | + protected override void OnSelectionChanged(SelectionChangedEventArgs e) |
| 64 | + { |
| 65 | + UpdateLinkText(); |
| 66 | + } |
| 67 | + |
| 68 | + private static void HeaderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 69 | + { |
| 70 | + var source = (LinkDropDown)d; |
| 71 | + source.UpdateLinkText(); |
| 72 | + } |
| 73 | + |
| 74 | + private void UpdateLinkText() |
| 75 | + { |
| 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 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments