2010-09-28 16 views
7

Ondalık girdi için bazı metin kutusuna sahip bir Wpf uygulamasına sahibim.Wpf TextBox'ta tuş takımı ondalık ayırıcı, nasıl yapılır?

PC klavyesinin sayısal tuş takımındaki "nokta" tuşuna (.) Bastığımda doğru ondalık ayırıcıyı gönderirim.

Örneğin, İtalyanca dilinde ondalık ayırıcı "virgül" (,) 'dir ... Mümkünse "virgül" karakterini göndermek için "nokta" tuşu ayarlanabilir mi?

cevap

3

Bunun için uygulamanızı gizlemeniz gerekir. Bu bağlantıları bakınız:

http://msdn.microsoft.com/en-us/library/ms788718.aspx

http://www.codeproject.com/KB/WPF/GlobalizeLocalizeAWPFPro.aspx

+0

Teşekkür gelebilir Eğer

public partial class MainWindow : Window { public MainWindow() { //Among other code if (CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator != ".") { //Handler attach - will not be done if not needed PreviewKeyDown += new KeyEventHandler(MainWindow_PreviewKeyDown); } } void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Decimal) { e.Handled = true; if (CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator.Length > 0) { Keyboard.FocusedElement.RaiseEvent( new TextCompositionEventArgs( InputManager.Current.PrimaryKeyboardDevice, new TextComposition(InputManager.Current, Keyboard.FocusedElement, CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator) ) { RoutedEvent = TextCompositionManager.TextInputEvent}); } } } } 

... Ben de beni yardımcı olan bir blog yazısı buldum: http: //www.metanous .be/pharcyde/post/Sayısal-ayırıcı-sayısal-ayırıcı-için-numeric-ayırıcı-için-in-in-in-in-in-in-in-the-excel.aspx –

+0

LukePet, psocyde/post/Use-the-sayısal tuş takımı, kırık lütfen, lütfen daha ayrıntılı olarak cevap verebilir misiniz Teşekkürler. – sthiers

+0

VB.NET kullanıcıları için: https://social.msdn.microsoft.com/Forums/vstudio/en-US/4bedf3c3-be4f-4b2a-b5e0-21c1a41caeca/how-to-use-decimal-separator-in-a -wpf-textbox-? forum = wpf – Naucle

4

Hızlı ve kirli:

private void NumericTextBox_KeyDown(object sender, KeyEventArgs e) { 
     if (e.Key == Key.Decimal) { 
      var txb = sender as TextBox; 
      int caretPos=txb.CaretIndex; 
      txb.Text = txb.Text.Insert(txb.CaretIndex, System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator); 
      txb.CaretIndex = caretPos + 1; 
      e.Handled = true; 
     } 
    } 
6

o dönüştürmek için yeterli değildir Mamta Dalal önerdiği gibi WPF varsayılan çevirici yerel ayarı rağmen "ondalık" tuşu doğru dizeye basın. Bu kod veriye bağlı denetimleri

//Will set up correct string formats for data-bound controls, 
// but will not replace numpad decimal key press 
private void Application_Startup(object sender, StartupEventArgs e) 
{ 
    //Among other settings, this code may be used 
    CultureInfo ci = CultureInfo.CurrentUICulture; 

    try 
    { 
     //Override the default culture with something from app settings 
     ci = new CultureInfo([insert your preferred settings retrieval method here]); 
    } 
    catch { } 
    Thread.CurrentThread.CurrentCulture = ci; 
    Thread.CurrentThread.CurrentUICulture = ci; 

    //Here is the important part for databinding default converters 
    FrameworkElement.LanguageProperty.OverrideMetadata(
      typeof(FrameworkElement), 
      new FrameworkPropertyMetadata(
       XmlLanguage.GetLanguage(ci.IetfLanguageTag))); 
    //Other initialization things 
} 

doğru para birimi simgesini ve tarih/saat biçimini gösterecektir ben PreviewKeyDown etkinliği pencere çapında taşıma metin kutusu-özgü biraz daha temiz (o takdirde daha iyi olurdu olduğunu bulduk uygulama çapında uygulanabilir). Herkes ... uygulama genelindeki ayarlamak için bir yol öneri için

+0

Bir çekicilik gibi çalışır! – Goldorak84

İlgili konular