Search code examples
c#wpfdatagridnumericupdown

NumericUpDown in Datagrid


I have a WPF window that holds a bunch of numbers. Or to use nicer words: Each column is bound to one int property. Now I want to have a nice way to change some of the numbers and I was hoping that I can use a numericUpDown control. Any hints would be appreciated!


Solution

  • You can use DataGridTemplateColumn.

    Sample code:

    <DataGrid ItemsSource="{Binding MyData}" AutoGenerateColumns="False">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                        <DataGridTemplateColumn Header="Age">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <wpfToolkit:IntegerUpDown Value="{Binding Age, UpdateSourceTrigger=PropertyChanged}" />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>
    

    Where wpfToolkit is:

    xmlns:wpfToolkit="clr-namespace:Xceed.Wpf.Toolkit;assembly=WPFToolkit.Extended"
    

    In this example I use IntegerUpDown control from Extended WPF Toolkit.