Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change MenuItem to inherit from HeaderedItemsControl #1616

Merged
merged 3 commits into from
Nov 15, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 11 additions & 38 deletions Microsoft.Toolkit.Uwp.UI.Controls/Menu/MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
/// <summary>
/// Menu Item is the items main container for Class Menu control
/// </summary>
public class MenuItem : ItemsControl
public class MenuItem : HeaderedItemsControl
{
private const string FlyoutButtonName = "FlyoutButton";
private const char UnderlineCharacter = '^';
Expand All @@ -48,43 +48,16 @@ public class MenuItem : ItemsControl

private Rect _bounds;

/// <summary>
/// Identifies the <see cref="Header"/> dependency property.
/// </summary>
public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(nameof(Header), typeof(object), typeof(MenuItem), new PropertyMetadata(null, HeaderPropertyChanged));

/// <summary>
/// Gets or sets the title to appear in the title bar
/// </summary>
public object Header
{
get { return GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}

private object InternalHeader
{
set
{
_isInternalHeaderUpdate = true;
Header = value;
_isInternalHeaderUpdate = false;
}
}

/// <summary>
/// Identifies the <see cref="HeaderTemplate"/> dependency property.
/// </summary>
public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register(nameof(HeaderTemplate), typeof(DataTemplate), typeof(MenuItem), new PropertyMetadata(null));

/// <summary>
/// Gets or sets the data template that is used to display the content of the MenuItem
/// </summary>
public DataTemplate HeaderTemplate
{
get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
set { SetValue(HeaderTemplateProperty, value); }
}

/// <summary>
/// Gets a value indicating whether the menu is opened or not
/// </summary>
Expand Down Expand Up @@ -484,19 +457,19 @@ internal void Underline()
InternalHeader = text;
}

private static void HeaderPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
/// <inheritdoc />
protected override void OnHeaderChanged(object oldValue, object newValue)
{
var menuItem = (MenuItem)sender;
base.OnHeaderChanged(oldValue, newValue);

if (menuItem._isInternalHeaderUpdate)
if (_isInternalHeaderUpdate)
{
menuItem._isInternalHeaderUpdate = false;
return;
}

menuItem._originalHeader = null;
_originalHeader = null;

var headerString = menuItem.Header as string;
var headerString = newValue as string;

if (string.IsNullOrEmpty(headerString))
{
Expand All @@ -512,12 +485,12 @@ private static void HeaderPropertyChanged(DependencyObject sender, DependencyPro

if (underlineCharacterIndex == headerString.Length - 1)
{
menuItem.InternalHeader = headerString.Replace(UnderlineCharacter.ToString(), string.Empty);
InternalHeader = headerString.Replace(UnderlineCharacter.ToString(), string.Empty);
return;
}

menuItem._originalHeader = headerString;
menuItem.InternalHeader = headerString.Replace(UnderlineCharacter.ToString(), string.Empty);
_originalHeader = headerString;
InternalHeader = headerString.Replace(UnderlineCharacter.ToString(), string.Empty);
}

internal void RemoveUnderline()
Expand Down