Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Jul 10, 2022
1 parent 29296fd commit cd85b43
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
35 changes: 35 additions & 0 deletions ModernWpf.Controls/NumberBox/NumberBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public partial class NumberBox : Control
static NumberBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NumberBox), new FrameworkPropertyMetadata(typeof(NumberBox)));

AutomationProperties.NameProperty.OverrideMetadata(typeof(NumberBox), new FrameworkPropertyMetadata(OnAutomationPropertiesNamePropertyChanged));
}

public NumberBox()
Expand Down Expand Up @@ -331,6 +333,32 @@ private void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArg
UpdateVisualStateForIsEnabledChange();
}

private static void OnAutomationPropertiesNamePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((NumberBox)d).ReevaluateForwardedUIAName();
}

private void ReevaluateForwardedUIAName()
{
if (m_textBox is { } textBox)
{
var name = AutomationProperties.GetName(this);
if (!string.IsNullOrEmpty(name))
{
// AutomationProperties.Name is a non empty string, we will use that value.
AutomationProperties.SetName(textBox, name);
}
else
{
if (Header is string headerAsString)
{
// Header is a string, we can use that as our UIA name.
AutomationProperties.SetName(textBox, headerAsString);
}
}
}
}

private void UpdateVisualStateForIsEnabledChange()
{
VisualStateManager.GoToState(this, IsEnabled ? "Normal" : "Disabled", false);
Expand Down Expand Up @@ -657,6 +685,11 @@ private void UpdateHeaderPresenterState()
{
// Header is not a string, so let's show header presenter
shouldShowHeader = true;
// When our header isn't a string, we use the NumberBox's UIA name for the textbox's UIA name.
if (m_textBox is { } textBox)
{
AutomationProperties.SetName(textBox, AutomationProperties.GetName(this));
}
}
}
if (HeaderTemplate is { } headerTemplate)
Expand All @@ -677,6 +710,8 @@ private void UpdateHeaderPresenterState()
{
m_headerPresenter.Visibility = shouldShowHeader ? Visibility.Visible : Visibility.Collapsed;
}

ReevaluateForwardedUIAName();
}

private void MoveCaretToTextEnd()
Expand Down
49 changes: 49 additions & 0 deletions test/ModernWpfTestApp/ApiTests/NumberBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using System.Windows.Controls.Primitives;
using System.Windows;
using System.Windows.Input;
using System.Windows.Automation.Peers;
using System.Windows.Automation;

#if USING_TAEF
using WEX.TestExecution;
Expand Down Expand Up @@ -174,6 +176,53 @@ public void VerifyIsEnabledChangeUpdatesVisualState()
});
}

[TestMethod]
public void VerifyUIANameBehavior()
{
NumberBox numberBox = null;
TextBox textBox = null;

RunOnUIThread.Execute(() =>
{
numberBox = new NumberBox();
Content = numberBox;
Content.UpdateLayout();

textBox = TestPage.FindVisualChildrenByType<TextBox>(numberBox)[0];
Verify.IsNotNull(textBox);
numberBox.Header = "Some header";
});

IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
VerifyUIAName("Some header");
numberBox.Header = new Button();
AutomationProperties.SetName(numberBox, "Some UIA name");
});

IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
VerifyUIAName("Some UIA name");
numberBox.Header = new Button();
});

IdleSynchronizer.Wait();

RunOnUIThread.Execute(() =>
{
VerifyUIAName("Some UIA name");
});

void VerifyUIAName(string value)
{
Verify.AreEqual(value, FrameworkElementAutomationPeer.CreatePeerForElement(textBox).GetName());
}
}

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

0 comments on commit cd85b43

Please sign in to comment.