【发布时间】:2018-03-11 12:52:51
【问题描述】:
我想强制用户在 UWP 应用(适用于 Windows 10)上的 TextBox 中输入的文本为大写。 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();
}
}
}
【问题讨论】:
-
请注意,由于提出了这个问题,UWP 似乎也实现了 CharacterCasing 属性。
标签: c# uwp converter uppercase