Created
November 7, 2016 21:55
-
-
Save grokys/be1e565e2d99bc687662598774a1a83c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.ComponentModel; | |
| using System.Windows; | |
| using System.Windows.Controls; | |
| namespace GitHub.UI | |
| { | |
| /// <summary> | |
| /// A ComboBox that displays as a link with a dropdown. | |
| /// </summary> | |
| public class LinkDropDown : ComboBox | |
| { | |
| /// <summary> | |
| /// Defines the <see cref="LinkText"/> property. | |
| /// </summary> | |
| static readonly DependencyPropertyKey LinkTextPropertyKey = | |
| DependencyProperty.RegisterReadOnly( | |
| "LinkText", | |
| typeof(string), | |
| typeof(LinkDropDown), | |
| new FrameworkPropertyMetadata(string.Empty)); | |
| /// <summary> | |
| /// Defines the <see cref="Header"/> property. | |
| /// </summary> | |
| public static readonly DependencyProperty HeaderProperty = | |
| HeaderedItemsControl.HeaderProperty.AddOwner( | |
| typeof(LinkDropDown), | |
| new FrameworkPropertyMetadata(typeof(LinkDropDown), HeaderChanged)); | |
| /// <summary> | |
| /// Defines the readonly <see cref="LinkText"/> property. | |
| /// </summary> | |
| public static readonly DependencyProperty LinkTextProperty = | |
| LinkTextPropertyKey.DependencyProperty; | |
| /// <summary> | |
| /// Initializes a new instance of the <see cref="LinkDropDown"/> class. | |
| /// </summary> | |
| public LinkDropDown() | |
| { | |
| DependencyPropertyDescriptor.FromProperty(SelectionBoxItemProperty, typeof(LinkDropDown)) | |
| .AddValueChanged(this, SelectionBoxItemChanged); | |
| } | |
| /// <summary> | |
| /// Initializes static members of the <see cref="LinkDropDown"/> class. | |
| /// </summary> | |
| static LinkDropDown() | |
| { | |
| DefaultStyleKeyProperty.OverrideMetadata( | |
| typeof(LinkDropDown), | |
| new FrameworkPropertyMetadata(typeof(LinkDropDown))); | |
| } | |
| /// <summary> | |
| /// Gets or sets a header to use as the link text when no item is selected. | |
| /// </summary> | |
| public object Header | |
| { | |
| get { return GetValue(HeaderProperty); } | |
| set { SetValue(HeaderProperty, value); } | |
| } | |
| /// <summary> | |
| /// Gets the text to display in the link. | |
| /// </summary> | |
| public string LinkText | |
| { | |
| get { return (string)GetValue(LinkTextProperty); } | |
| private set { SetValue(LinkTextPropertyKey, value); } | |
| } | |
| protected override void OnSelectionChanged(SelectionChangedEventArgs e) | |
| { | |
| UpdateLinkText(); | |
| } | |
| private static void HeaderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
| { | |
| var source = (LinkDropDown)d; | |
| source.UpdateLinkText(); | |
| } | |
| void SelectionBoxItemChanged(object sender, EventArgs e) | |
| { | |
| var source = (LinkDropDown)sender; | |
| source.UpdateLinkText(); | |
| } | |
| private void UpdateLinkText() | |
| { | |
| if (SelectedItem != null) | |
| { | |
| LinkText = SelectionBoxItem?.ToString(); | |
| } | |
| else | |
| { | |
| LinkText = Header.ToString(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment