WPF中的所有输入自动大写

发布于 2024-07-18 19:12:45 字数 33 浏览 3 评论 0 原文

有没有办法在 WPF 应用程序中自动将所有输入大写?

Is there a way to automatically capitalize all input throughout a WPF app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

红玫瑰 2024-07-25 19:12:45

您可以使用以下属性对 TextBox 控件中的所有输入进行大小写:

CharacterCasing="Upper"

要应用于整个应用程序中的所有 TextBox 控件,请为所有 TextBox 创建一个样式控制:

<Style TargetType="{x:Type TextBox}">
    <Setter Property="CharacterCasing" Value="Upper"/>
</Style>

You can case all input into TextBox controls with the following property:

CharacterCasing="Upper"

To apply to all TextBox controls in the entire application create a style for all TextBox controls:

<Style TargetType="{x:Type TextBox}">
    <Setter Property="CharacterCasing" Value="Upper"/>
</Style>
堇年纸鸢 2024-07-25 19:12:45

如果您想将单个 TextBox 的输入大写,而不是像 TextBoxes >上面,您可以使用以下内容:

<TextBox CharacterCasing="Upper"/>

If you want to capitalize the input for a single TextBox rather than all TextBoxes like above, you can use the following:

<TextBox CharacterCasing="Upper"/>
糖果控 2024-07-25 19:12:45

我建议创建一个自定义 Textbox 类并覆盖一个事件以自动将文本大写。 首先,这取决于您是否希望文本在键入时或输入完成后大写。

例如输入完成后

public class AutoCapizalizeTextBox: TextBox
{
  public AutoCapitalizeTextBox()
  {
  }

  public AutoCapitlizeTextBox()
  {
  }

  protected override void OnLostFocus(EventArgs e)
  {
    this.Text = this.Text.ToUpper();

    base.OnLostFocus(e);
  }
}

I recommend creating a custom Textbox class and override an event to automatically capitalize the text. First, this depends on if you want the text to be capitalize as they type or after input is finished.

E.g. for after input is finished

public class AutoCapizalizeTextBox: TextBox
{
  public AutoCapitalizeTextBox()
  {
  }

  public AutoCapitlizeTextBox()
  {
  }

  protected override void OnLostFocus(EventArgs e)
  {
    this.Text = this.Text.ToUpper();

    base.OnLostFocus(e);
  }
}
浮萍、无处依 2024-07-25 19:12:45

我不知道这是否有帮助,它将句子中的所有第一个字母大写。

http://www.mardymonkey.co .uk/blog/auto-capitalise-a-text-control-in-wpf/

I don't know if this'll help, it capitalizes all the first letters in the sentence.

http://www.mardymonkey.co.uk/blog/auto-capitalise-a-text-control-in-wpf/

岁月苍老的讽刺 2024-07-25 19:12:45

也许你可以使用转换器。
这是转换器的代码:

using System;
using System.Globalization;
using System.Windows.Data;
namespace SistemaContable.GUI.WPF.Converters
{
    public class CapitalizeFirstLetter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                string stringToTitleCase = culture.TextInfo.ToTitleCase(value.ToString());
                return stringToTitleCase;
            }
            else
            {
                return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.ToString();
        }
    }
}

您需要在“ResourceDictionary”或“App.xaml”中引用它:

<ResourceDictionary xmlns:converters="clr-namespace:SistemaContable.GUI.WPF.Converters">
    <converters:CapitalizeFirstLetter x:Key="CapitalizeFirstLetter"/>
</ResourceDictionary>

您可以像这样使用它:

<TextBox x:Name="txtNombre" Text="{Binding Usuario.Nombre, Converter={StaticResource CapitalizeFirstLetter}, UpdateSourceTrigger=PropertyChanged}"/>

Perhaps you can use a converter.
Here's the code of the converter:

using System;
using System.Globalization;
using System.Windows.Data;
namespace SistemaContable.GUI.WPF.Converters
{
    public class CapitalizeFirstLetter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                string stringToTitleCase = culture.TextInfo.ToTitleCase(value.ToString());
                return stringToTitleCase;
            }
            else
            {
                return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.ToString();
        }
    }
}

You need to reference it in a "ResourceDictionary" or in your "App.xaml":

<ResourceDictionary xmlns:converters="clr-namespace:SistemaContable.GUI.WPF.Converters">
    <converters:CapitalizeFirstLetter x:Key="CapitalizeFirstLetter"/>
</ResourceDictionary>

And you can use it like this:

<TextBox x:Name="txtNombre" Text="{Binding Usuario.Nombre, Converter={StaticResource CapitalizeFirstLetter}, UpdateSourceTrigger=PropertyChanged}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文