在 UWP 中强制文本框为大写 (Windows 10)

Force textbox to be uppercase in UWP (Windows 10)

我想强制用户在 UWP 应用程序(对于 Windows 10)上输入文本框的文本为大写。 WinForms 和 WPF 都有一种使用内置 CharacterCasing 执行此操作的简单方法,但 UWP 没有。

我尝试了两种不同的方法; AlexDrenea 的在线示例,我自己构建了一个转换器。在这两种情况下,我发现当我在文本框中键入 "test" 时,文本会变得混乱(例如,"test" 显示为 "TSTE")。

我真的认为转换器会起作用。关于可以做些什么来改进它以免字母混乱的任何建议?

XAML

<Page
    x:Class="MakeUppercaseEx2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MakeUppercaseEx2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <local:TextToUppercaseConverter x:Name="MyUppercaseConverter" />
    </Page.Resources>

    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBox Name="txtExample2" Margin="10" Text="{Binding ElementName=txtExample2, Path=Text, Converter={StaticResource MyUppercaseConverter}}" />
        <Button Name="btnDisplay" Margin="10" Content="Display" Click="btnDisplay_Click"/>
        <TextBlock Name="lblStatus" Margin="10" Text="" />
    </StackPanel>
</Page>

代码隐藏

using System;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace MakeUppercaseEx2
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void btnDisplay_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) {
            lblStatus.Text = "You entered: " + txtExample2.Text;
        }
    }

    public class TextToUppercaseConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, string language) {
            string sTypedValue = System.Convert.ToString(value);
            if (string.IsNullOrEmpty(sTypedValue)) {
                return "";
            }
            return sTypedValue.ToUpper();
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language) {
            throw new NotImplementedException();
        }
    }
}

这是我的做法。

我将创建一个继承自 TextBoxTemplatedControl,并将管理 TemplatedControl 内部的 TextChanged 事件,以便它可以在任何地方使用。另外我会制作一个 属性 来确定我是否希望文本始终大写。这样我就可以在所有必要的地方使用相同的控件。

下面是一个简单的TextBox继承TemplatedControl

public sealed class MyTextBox : TextBox
{
    public MyTextBox()
    {
        this.DefaultStyleKey = typeof(TextBox);
    }

    public CaseType CharacterCasing
    {
        get { return (CaseType)GetValue(CharacterCasingProperty); }
        set { SetValue(CharacterCasingProperty, value); }
    }

    public static readonly DependencyProperty CharacterCasingProperty = DependencyProperty.Register("CharacterCasing", typeof(CaseType), typeof(MyTextBox), new PropertyMetadata(CaseType.Normal,(s,e)=>
    {
        TextBox myTextBox = (TextBox)s;
        if ((CaseType)e.NewValue !=(CaseType)e.OldValue)
        {
            myTextBox.TextChanged += MyTextBox_TextChanged;
        }
        else
        {
            myTextBox.TextChanged -= MyTextBox_TextChanged;
        }
    }));

    private static void MyTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        MyTextBox myTextBox = sender as MyTextBox;
        switch(myTextBox.CharacterCasing)
        {
            case CaseType.UpperCase:
                myTextBox.Text = myTextBox.Text.ToUpper();
                break;
            case CaseType.LowerCase:
                myTextBox.Text = myTextBox.Text.ToLower();
                break;
            default:
                break;
        }
        myTextBox.SelectionStart = myTextBox.Text.Length;
    }

    public enum CaseType
    {
        Normal,
        UpperCase,
        LowerCase
    }
}

我创建了一个名为 CaseType 的枚举,它将确定 TextBox 在用户键入文本时应遵循的大小写。

并且在 TextChanged 事件中,我正在根据 Enum.

对文本进行 CaseConverting

您可以在 XAML 中使用它,如下所示。

<local:MyTextBox HorizontalAlignment="Center"
                 VerticalAlignment="Center"
                 CharacterCasing="UpperCase"/>

您可以根据需要使用更多 CaseConverting 选项扩展控件。

祝你好运。


更新

这是一个 Github 回购协议。随意下载并与您正在做的事情进行比较。

我使用文本框的CharacterReceived事件

private void TextBox_CharacterReceived(UIElement sender, CharacterReceivedRoutedEventArgs args)
        {
            TextBox TBx = (TextBox)sender;

            if (char.IsLower(args.Character))
            {
                int Pos = TBx.SelectionStart;
                TBx.Text = TBx.Text.ToUpper();
                TBx.SelectionStart = Pos;
                args.Handled = true;
            }
        }