wpf datagrid : create a DatagridNumericColumn in wpf

前端 未结 6 915
迷失自我
迷失自我 2021-02-14 11:33

I have a requirement that I want to make a datagridcolumn which only accepts numeric values(integer) ,when the user enter something other than numbers handle the textbox . I tri

6条回答
  •  梦毁少年i
    2021-02-14 12:01

    If you dont want to show any validation errors and just want to block any non-numeral value then you can create the DataGridTemplateColumn and in CellEditingTemplate use the TextBox.

                    
                        
                            
                                
                            
                        
                        
                            
                                
                            
                        
                    
    

    and in PreviewTextInput of the TextBox set e.Handled = true if value is other than integer:

           private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
            {
                try
                {
                    Convert.ToInt32(e.Text);
                }
                catch
                {
                    e.Handled = true;
                }
            }
    

提交回复
热议问题