Skip to content

Commit

Permalink
Add InputScope Property To NumberBox (microsoft/microsoft-ui-xaml#3186)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Oct 12, 2020
1 parent d752131 commit 89bc3d6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
15 changes: 15 additions & 0 deletions ModernWpf.Controls/NumberBox/NumberBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ public NumberBox()

GotKeyboardFocus += OnNumberBoxGotFocus;
LostKeyboardFocus += OnNumberBoxLostFocus;

SetDefaultInputScope();
}

void SetDefaultInputScope()
{
// Sets the default value of the InputScope property.
// Note that InputScope is a class that cannot be set to a default value within the IDL.
var inputScopeName = new InputScopeName(InputScopeNameValue.Number);
var inputScope = new InputScope();
inputScope.Names.Add(inputScopeName);

SetValue(InputScopeProperty, inputScope);

return;
}

private INumberBoxNumberFormatter GetRegionalSettingsAwareDecimalFormatter()
Expand Down
2 changes: 1 addition & 1 deletion ModernWpf.Controls/NumberBox/NumberBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@
<TextBox
x:Name="InputBox"
Grid.Row="1"
InputScope="Number"
InputScope="{TemplateBinding InputScope}"
ui:ControlHelper.PlaceholderText="{TemplateBinding PlaceholderText}"
SelectionBrush="{TemplateBinding SelectionBrush}"
FontSize="{TemplateBinding FontSize}"
Expand Down
29 changes: 29 additions & 0 deletions test/ModernWpfTestApp/ApiTests/NumberBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using ModernWpf.Controls;
using System.Windows.Controls.Primitives;
using System.Windows;
using System.Windows.Input;

#if USING_TAEF
using WEX.TestExecution;
Expand Down Expand Up @@ -108,6 +109,34 @@ public void VerifyNumberBoxCornerRadius()
});
}

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

RunOnUIThread.Execute(() =>
{
Content.UpdateLayout();
var inputTextBox = TestUtilities.FindDescendents<TextBox>(numberBox).Where(e => e.Name == "InputBox").Single();
Verify.AreEqual(1, inputTextBox.InputScope.Names.Count);
Verify.AreEqual(InputScopeNameValue.Number, ((InputScopeName)inputTextBox.InputScope.Names[0]).NameValue, "The default InputScope should be 'Number'.");
var scopeName = new InputScopeName();
scopeName.NameValue = InputScopeNameValue.CurrencyAmountAndSymbol;
var scope = new InputScope();
scope.Names.Add(scopeName);
numberBox.InputScope = scope;
Content.UpdateLayout();
Verify.AreEqual(1, inputTextBox.InputScope.Names.Count);
Verify.AreEqual(InputScopeNameValue.CurrencyAmountAndSymbol, ((InputScopeName)inputTextBox.InputScope.Names[0]).NameValue, "The InputScope should be 'CurrencyAmountAndSymbol'.");
});

return;
}

[TestMethod]
public void VerifyIsEnabledChangeUpdatesVisualState()
{
Expand Down
8 changes: 8 additions & 0 deletions test/ModernWpfTestApp/NumberBoxPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
<ComboBoxItem Content="Right" />
</ComboBoxEx>

<ComboBoxEx x:Name="InputScopeComboBox" AutomationProperties.Name="InputScopeComboBox" Header="InputScope" SelectedIndex="0" SelectionChanged="InputScope_Changed">
<ComboBoxItem Content="Number" />
<ComboBoxItem Content="Default" />
<ComboBoxItem Content="CurrencyAmountAndSymbol" />
<ComboBoxItem Content="TelephoneNumber" />
<ComboBoxItem Content="Formula" />
</ComboBoxEx>

<CheckBox x:Name="EnabledCheckBox" AutomationProperties.Name="EnabledCheckBox" IsChecked="True" Content="Enabled"/>

<CheckBox x:Name="ExpressionCheckBox" AutomationProperties.Name="ExpressionCheckBox" IsChecked="False" Content="Accepts Expression"/>
Expand Down
20 changes: 20 additions & 0 deletions test/ModernWpfTestApp/NumberBoxPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Windows.Markup;
using MUXControlsTestApp.Utilities;
using ModernWpf;
using System.Windows.Input;

namespace MUXControlsTestApp
{
Expand All @@ -29,6 +30,25 @@ public NumberBoxPage()
TestNumberBox.RegisterPropertyChangedCallback(NumberBox.TextProperty, new DependencyPropertyChangedCallback(TextPropertyChanged));
}

private void InputScope_Changed(object sender, RoutedEventArgs e)
{
if (TestNumberBox != null &&
sender is ComboBox comboBox &&
comboBox.SelectedItem is ComboBoxItem item)
{
var scopeName = new InputScopeName();
scopeName.NameValue = (InputScopeNameValue)Enum.Parse(typeof(InputScopeNameValue), item.Content?.ToString() ?? string.Empty, true);

var scope = new InputScope();
scope.Names.Add(scopeName);

TestNumberBox.InputScope = scope;

// Help testing by returning focus to the NumberBox to see the keyboard change
TestNumberBox.Focus(/*FocusState.Keyboard*/);
}
}

private void SpinMode_Changed(object sender, RoutedEventArgs e)
{
if (TestNumberBox != null)
Expand Down

0 comments on commit 89bc3d6

Please sign in to comment.