Skip to content

Commit

Permalink
Merge pull request #1697 from r2d2rigo/menuitem-disabled-state
Browse files Browse the repository at this point in the history
MenuItem disabled state
  • Loading branch information
nmetulev authored Jan 19, 2018
2 parents 9b92045 + df6dec3 commit 7387986
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 1 addition & 2 deletions Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Menu/Menu.bind
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

<controls:Menu AllowTooltip="@[AllowTooltip:Bool:False]"
Orientation="@[Orientation:Enum:Orientation.Horizontal]"
TooltipPlacement="@[TooltipPlacement:Enum:PlacementMode.Bottom]">

TooltipPlacement="@[TooltipPlacement:Enum:PlacementMode.Bottom]">
<controls:MenuItem Name="FileMenu"
controls:Menu.InputGestureText="Alt+F"
Header="^File">
Expand Down
19 changes: 19 additions & 0 deletions Microsoft.Toolkit.Uwp.UI.Controls/Menu/Menu.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@
<PointerUpThemeAnimation Storyboard.TargetName="FlyoutButton" />
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="FlyoutButton"
Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{ThemeResource SystemControlDisabledTransparentBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="FlyoutButton"
Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="FlyoutButton"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
Expand Down
23 changes: 23 additions & 0 deletions Microsoft.Toolkit.Uwp.UI.Controls/Menu/MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ protected override void OnApplyTemplate()
IsOpened = false;

Items.VectorChanged -= Items_VectorChanged;
IsEnabledChanged -= MenuItem_IsEnabledChanged;

if (MenuFlyout == null)
{
Expand All @@ -128,16 +129,26 @@ protected override void OnApplyTemplate()
MenuFlyout.MenuFlyoutPresenterStyle = _parentMenu.MenuFlyoutStyle;
ReAddItemsToFlyout();

IsEnabledChanged += MenuItem_IsEnabledChanged;

if (_isAccessKeySupported)
{
FlyoutButton.AccessKey = AccessKey;
AccessKey = string.Empty;
}
}

UpdateEnabledVisualState();

base.OnApplyTemplate();
}

private void MenuItem_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var menuItemControl = (MenuItem)sender;
menuItemControl.UpdateEnabledVisualState();
}

internal void CalculateBounds()
{
var ttv = TransformToVisual(Window.Current.Content);
Expand Down Expand Up @@ -500,5 +511,17 @@ internal void RemoveUnderline()
InternalHeader = _originalHeader.Replace(UnderlineCharacter.ToString(), string.Empty);
}
}

internal void UpdateEnabledVisualState()
{
if (IsEnabled)
{
VisualStateManager.GoToState(this, "Normal", true);
}
else
{
VisualStateManager.GoToState(this, "Disabled", true);
}
}
}
}

0 comments on commit 7387986

Please sign in to comment.