2017-09-29 7 views
3

UWP 앱 (Windows 10)의 TextBox에 사용자가 입력 한 텍스트를 대문자로 만들려합니다. WinForms와 WPF는 모두 CharacterCasing을 사용하여이 작업을 수행하는 간단한 방법을 제공하지만 UWP는 그렇지 않습니다.UWP에서 텍스트 상자를 대문자로 지정하십시오 (Windows 10)

두 가지 방법을 시도했습니다. 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(); 
     } 
    } 
} 

답변

2

여기에 내가 할 것이 방법입니다. 내가 TextBox에서 상속하고 어디에서나 사용할 수 있도록 TemplatedControl 내부 TextChanged 이벤트를 관리하는 TemplatedControl를 생성합니다

. 또한 텍스트를 항상 대문자로 표시할지 여부를 결정하는 Property를 만들 것입니다. 그렇게하면 모든 장소에서 동일한 컨트롤을 사용할 수 있습니다.

다음은 간단한 텍스트 상자가 나는 열거는 사용자가 텍스트를 입력 할 때 TextBox가 존중해야 어떤 경우 결정하는 것이다 CaseType라고 만든

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 
    } 
} 

TemplatedControl을 상속됩니다.

TextChanged 이벤트에서 나는 Enum을 기준으로 텍스트를 CaseConverting합니다.

다음과 같이 XAML에서 사용할 수 있습니다.

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

요구 사항에 따라 CaseConverting에 대한 더 많은 옵션으로 컨트롤을 확장 할 수 있습니다.

행운을 비네.


업데이트 여기

Github 리포이다. 언제든지 다운로드하고 비교할 수 있습니다.

+2

TextChanging 이벤트를 사용하여 입력 된 텍스트를 가져 와서 대문자 버전을 반환 할 수 있습니다. 이 이벤트를 사용하면 원래의 소문자가 TextChanged와 같이 입력되고 제거되지 않는다는 것을 알 수 있습니다. – jsmyth886

+1

@ jsmyth886 WPF에서는 'CharacterCasing'을 'Upper'로 설정했기 때문에 'TextChanging' 대신'TextChanged'를 명시 적으로 사용했습니다. 당신이 뭔가를 입력하면, 그것은 원래의 입력 된 텍스트를 아래쪽에 표시하지 않고 상단으로 변경합니다. 똑바로 대문자를 표시합니다. 나는 여기에서 같은 것을 반복하고 싶었다. – AVK

+0

@AVK - 코드가 작동하고 대문자로 변경되지만 코드 숨김에서 새 컨트롤에 액세스 할 수 없습니다. 어쩌면 내가 뭔가를 놓치고 있지만 코드 비하인드에서 텍스트 값을 얻으려고하면 (예 : string sWhatWasTyped = txtExample.본문;) 오류가 발생합니다 : 이름 'txtExample2'현재 컨텍스트에서 존재하지 않습니다. – ThePeter