Wpf 文本框上的键盘小数点分隔符如何?

发布于 2024-09-25 20:10:18 字数 146 浏览 5 评论 0原文

我有一个 Wpf 应用程序,带有一些用于十进制输入的文本框。

我希望当我按电脑键盘数字键盘上的“点”键 (.) 时,它会发送正确的小数分隔符。

例如,在意大利语中,小数点分隔符是“逗号”(,)...是否可以设置“点”键以在按下时发送“逗号”字符?

I have a Wpf application with some textbox for decimal input.

I would that when I press "dot" key (.) on numeric keypad of pc keyboard it send the correct decimal separator.

For example, on Italian language the decimal separator is "comma" (,)...Is possible set the "dot" key to send the "comma" character when pressed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱格式化 2024-10-02 20:10:18

又快又脏:

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

Quick and dirty:

   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;
        }
    }
愁杀 2024-10-02 20:10:18

尽管您可以按照 Mamta Dalal 的建议在 WPF 中设置默认转换器区域设置,但这不足以将“十进制”按键转换为正确的字符串。此代码将在数据绑定控件上显示正确的货币符号和日期/时间格式

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

我发现在窗口范围内处理 PreviewKeyDown 事件比特定于文本框的事件要干净一些(如果可以在应用程序范围内应用它会更好) 。

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

如果有人能想出一种在应用程序范围内设置它的方法......

Although you may set the default converter locale in WPF as suggested by Mamta Dalal it is not enough to convert the "decimal" key press to the correct string. This code will display the correct currency symbol and date/time format on data-bound controls

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

I found that handling the previewKeyDown event window-wide is a little cleaner than textbox-specific (it would be better if it could be applied application-wide).

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

If anybody could come up with a way to set it application-wide...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文