Based on the @nit clause, you can create your own class derived from a DataGridTextColumn as follows:
public class DataGridNumericColumn : DataGridTextColumn { protected override object PrepareCellForEdit(System.Windows.FrameworkElement editingElement, System.Windows.RoutedEventArgs editingEventArgs) { TextBox edit = editingElement as TextBox; edit.PreviewTextInput += OnPreviewTextInput; return base.PrepareCellForEdit(editingElement, editingEventArgs); } void OnPreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e) { try { Convert.ToInt32(e.Text); } catch {
In the PrepareCellForEdit method , you register the OnPreviewTextInput method to edit the TextBox PreviewTextInput , where you check the numeric values.
In xaml you just use it:
<DataGrid ItemsSource="{Binding SomeCollection}"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding NonNumericProperty}"/> <local:DataGridNumericColumn Binding="{Binding NumericProperty}"/> </DataGrid.Columns> </DataGrid>
Hope this helps
Omri btian
source share