简体   繁体   English

如何在Windows Phone TextBox Wpf C#中键入大写字符串

[英]how To Type Uppercase String in windows phone TextBox Wpf c#

how To Type Uppercase String in windows phone TextBox Wpf c#. 如何在Windows Phone TextBox Wpf c#中键入大写字符串。

I tried it on KeyDown Event. 我在KeyDown事件上尝试过。

   void TxtPanno_KeyDown(object sender, KeyEventArgs e)  
   {

   TxtPanno.Text = TxtPanno.Text.ToUpper();  //1 code.

   TxtPanno.Text= CultureInfo.CurrentCulture.TextInfo.ToUpper(TxtPanno.Text); //2 code     

   TxtPanno.Text=Regex.Replace(TxtPanno.Text, "^[A-Z]", m => m.Value.ToUpper());  //3 code. 
   }

But Problems is that. 但是问题在于。

Cursor always go to leftside. 光标总是移到左侧。

Try doing: 尝试做:

TxtPanno.Text = TxtPanno.Text.ToUpper(); 
TxtPanno.SelectionStart = TxtPanno.Text.Length;
TxtPanno.SelectionLength = 0;

It makes the text uppercase, moves the cursor to the end and selects nothing. 它使文本变为大写,将光标移到末尾,并且什么也没有选择。

You should not try to replace text,instead you should apply styles like this 您不应该尝试替换文本,而应该应用这样的样式

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

The above style will make all textboxes uppercase, you can change it to only apply to a specific textbook like this 上面的样式将使所有文本框都变为大写,您可以将其更改为仅适用于这样的特定教科书

<TextBox CharacterCasing="Upper" />

then when using the text entered, make it upper case. 然后在使用输入的文本时将其大写。

Finally I tried this code and it worked. 最终,我尝试了这段代码,它成功了。

void TxtPanno_TextChanged(object sender, TextChangedEventArgs e)
{

  TxtPanno.Text = TxtPanno.Text.ToUpper();
  TxtPanno.SelectionStart = TxtPanno.Text.Length;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM