2010-09-28 33 views
7

Tôi có một ứng dụng Wpf với một số hộp văn bản để nhập số thập phân.Dấu tách thập phân bàn phím trên Hộp văn bản Wpf, cách thực hiện?

Tôi sẽ làm điều đó khi tôi nhấn phím "chấm" (.) Trên bàn phím số của bàn phím máy tính, nó sẽ gửi dấu phân cách thập phân chính xác.

Ví dụ: trên ngôn ngữ Ý dấu phân cách thập phân là "dấu phẩy" (,) ... Có thể đặt khóa "dấu chấm" để gửi ký tự "dấu phẩy" khi được nhấn không?

Trả lời

3

Để làm điều đó, bạn cần phải áp dụng cho ứng dụng của mình. Xem những liên kết này:

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

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

+0

Cảm ơn bạn đã gợi ý ... Tôi cũng tìm thấy một bài viết trên blog đó giúp tôi: http: //www.metanous .be/pharcyde/post/Sử dụng-số-bàn phím-số thập phân-phím-như-số-dấu phân cách-cho-tất-cả-miền---trong-Excel.aspx –

+0

LukePet, liên kết được khoanh vùng bị hỏng, xin vui lòng bạn có thể trả lời chi tiết hơn không. Cảm ơn. – sthiers

+0

Dành cho người dùng VB.NET: 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

Nhanh chóng và bẩn:

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

Mặc dù bạn có thể thiết lập miền địa phương chuyển đổi mặc định trong WPF theo đề nghị của Mamta Dalal nó là không đủ để chuyển đổi phím "thập phân" nhấn vào chuỗi chính xác. Mã này sẽ hiển thị các ký hiệu tiền tệ đúng đắn và định dạng ngày/giờ trên điều khiển dữ liệu-bound

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

tôi thấy rằng xử lý PreviewKeyDown kiện cửa sổ rộng là một chút sạch hơn textbox cụ thể (sẽ tốt hơn nếu nó có thể được áp dụng rộng rãi).

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

Nếu ai có thể đưa ra một cách để thiết lập nó ứng dụng rộng ...

+0

Làm việc như một sự quyến rũ! – Goldorak84

Các vấn đề liên quan