Numeric Data Entry in WPF

前端 未结 17 2583
情话喂你
情话喂你 2020-12-02 05:38

How are you handling the entry of numeric values in WPF applications?

Without a NumericUpDown control, I\'ve been using a TextBox and handling its PreviewKeyDown eve

17条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 06:05

    void PreviewTextInputHandler(object sender, TextCompositionEventArgs e)
    {
        string sVal = e.Text;
        int val = 0;
    
        if (sVal != null && sVal.Length > 0)
        {
            if (int.TryParse(sVal, out val))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
    

提交回复
热议问题