我正在创建一个wpf应用程序。我必须把所有的文本框首字母大写,如果用户输入小,那么它应该用鼠标大写格式化。我需要最好的方式来做到这一点,请别人帮助我。自动大写文本框中的第一个字母
1
A
回答
3
极大地做它的最佳方法取决于你如何做你的应用程序,但@ H.B.的答案很可能是要走的路。
为了完整起见,另一种方式,如果这样做是使用像这样的转换器:
<!-- Your_Window.xaml -->
<Window x:Class="..."
...
xmlns:cnv="clr-namespace:YourApp.Converters">
<Window.Resources>
<cnv.CapitalizeFirstLetterConverter x:Key="capFirst" />
</Window.Resources>
...
<TextBox Text="{Binding Path=SomeProperty, Converter={StaticResource capFirst}}" />
这里假设你的窗口的数据上下文设置为具有读取一个类的实例/写名为类型为字符串的SomeProperty的属性。 转换器本身将是这样的:
// CapitalizeFirstLetterConverter.cs
using System;
using System.Data;
using System.Globalization;
namespace YourApp.Converters {
[ValueConversion(typeof(string), typeof(string))]
public class CapitalizeFirstLetterConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
// this will be called after getting the value from your backing property
// and before displaying it in the textbox, so we just pass it as-is
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
// this will be called after the textbox loses focus (in this case) and
// before its value is passed to the property setter, so we make our
// change here
if (value is string) {
var castValue = (string)value;
return char.ToUpper(castValue[0]) + castValue.Substring(1);
}
else {
return value;
}
}
}
}
您可以了解更多有关转换器here。
1
您可以将样式放入Application.Resources
以处理所有TextBoxes
上的LostFocus
,那么您只需相应地更改Text属性。
<!-- App.xaml - Application.Resources -->
<Style TargetType="{x:Type TextBox}">
<EventSetter Event="LostFocus" Handler="TextBox_LostFocus" />
</Style>
// App.xaml.cs - App
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
var tb = (TextBox)sender;
if (tb.Text.Length > 0)
{
tb.Text = Char.ToUpper(tb.Text[0]) + tb.Text.Substring(1);
}
}
1
我对游戏有点迟,但如果有其他人需要它,这个DLL会实时大写第一个字母。例如,你不需要将鼠标移出。
http://www.mardymonkey.co.uk/blog/auto-capitalise-a-text-control-in-wpf/
相关问题
- 1. 键盘的大写字母锁定文本框的第一个字母的键?
- 2. 大写字母的第一个字母
- 3. 在文本框中输入文本时,我想让文本的第一个字母自动大写
- 4. 第一个字母大写
- 5. 大写数组中的第一个字母的文本
- 6. 第一个字母大写小写
- 7. Swift大写字母中每个单词的第一个字母
- 8. 将文件名第一个字母大写的脚本
- 9. 大写字母中的第一个字母?
- 10. 的首字母大写只有大写第一个字的f.label
- 11. grep第一个和自由文本格式的小写字母
- 12. 如何大写自定义textview中的第一个字母?
- 13. 大写字母的字符串的第一个字母只
- 14. 大写第一个字母大写但不改变大写
- 15. 寻找第一个大写字母
- 16. BASH - 让第一个字母大写
- 17. XSLT2.0第一个字母大写
- 18. C使第一个字母大写
- 19. 大写每个大写单词的第一个字母
- 20. 仅在Python中使用第一个字母大写字母
- 21. 大写文件中的第一个字母
- 22. 检查文件中的第一个字母大写
- 23. 将文件中每行的第一个字母改为大写
- 24. 如何大写第一个字母的文字
- 25. 如何匹配的第一个字母是大写/大写字母在PHP?
- 26. Javascript/jQuery大写变音字母的第一个字母
- 27. EditText的第一个字母大写字母
- 28. 的strftime使第一个字母大写(大写)在PHP
- 29. 自动大写化合物名称中的第一个字母(EditText,Android)
- 30. 大写列表中每个字符串的第一个字母大写
谢谢老兄,我一直在寻找somethink这样............ – Anu
此外,在文本绑定,如果你把'UpdateSourceTrigger = PropertyChanged',该转换器将运行而不是当TextBox失去焦点时。 –