Skip to content

Commit

Permalink
NumberBox: Fix header foreground color not being properly updated whe…
Browse files Browse the repository at this point in the history
…n NumberBox is disabled. (microsoft/microsoft-ui-xaml#3005)
  • Loading branch information
Kinnara committed Aug 17, 2020
1 parent 468801a commit a6fab17
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ModernWpf.Controls/NumberBox/NumberBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,16 @@ TextBox GetTextBox()
popupSpinUp.Click += OnSpinUpClick;
}

IsEnabledChanged += OnIsEnabledChanged;

// .NET rounds to 12 significant digits when displaying doubles, so we will do the same.
//m_displayRounder.SignificantDigits(12);

UpdateSpinButtonPlacement();
UpdateSpinButtonEnabled();

UpdateVisualStateForIsEnabledChange();

if (ReadLocalValue(ValueProperty) == DependencyProperty.UnsetValue
&& ReadLocalValue(TextProperty) != DependencyProperty.UnsetValue)
{
Expand Down Expand Up @@ -304,6 +308,16 @@ private void OnValidationModePropertyChanged(DependencyPropertyChangedEventArgs
UpdateSpinButtonEnabled();
}

private void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs args)
{
UpdateVisualStateForIsEnabledChange();
}

private void UpdateVisualStateForIsEnabledChange()
{
VisualStateManager.GoToState(this, IsEnabled ? "Normal" : "Disabled", false);
}

private void OnNumberBoxGotFocus(object sender, RoutedEventArgs e)
{
// When the control receives focus, select the text
Expand Down
4 changes: 4 additions & 0 deletions ModernWpf.Controls/NumberBox/NumberBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@

<Grid ui:ThemeManager.HasThemeResources="True">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Disabled" />
</VisualStateGroup>
<VisualStateGroup x:Name="SpinButtonStates">
<VisualState x:Name="SpinButtonsCollapsed" />
<VisualState x:Name="SpinButtonsVisible" />
Expand Down
37 changes: 37 additions & 0 deletions test/ModernWpfTestApp/ApiTests/NumberBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,43 @@ public void VerifyNumberBoxCornerRadius()
});
}

[TestMethod]
public void VerifyIsEnabledChangeUpdatesVisualState()
{
var numberBox = SetupNumberBox();

VisualStateGroup commonStatesGroup = null;
RunOnUIThread.Execute(() =>
{
// Check 1: Set IsEnabled to true.
numberBox.IsEnabled = true;
Content.UpdateLayout();
var numberBoxLayoutRoot = (FrameworkElement)VisualTreeHelper.GetChild(numberBox, 0);
commonStatesGroup = VisualStateManager.GetVisualStateGroups(numberBoxLayoutRoot).Cast<VisualStateGroup>().First(vsg => vsg.Name.Equals("CommonStates"));
Verify.AreEqual("Normal", commonStatesGroup.CurrentState.Name);
// Check 2: Set IsEnabled to false.
numberBox.IsEnabled = false;
});
IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
Verify.AreEqual("Disabled", commonStatesGroup.CurrentState.Name);
// Check 3: Set IsEnabled back to true.
numberBox.IsEnabled = true;
});
IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
Verify.AreEqual("Normal", commonStatesGroup.CurrentState.Name);
});
}

private NumberBox SetupNumberBox()
{
NumberBox numberBox = null;
Expand Down

0 comments on commit a6fab17

Please sign in to comment.