Search code examples
c#wpfc#-4.0wpfdatagrid

How to limit WPF DataGridTextColum Text max length to 10 characters


How can I limit WPF DataGridTextColumn Text to max length of 10 characters.

I don't want to use DatagridTemplateColumn, because it has memory leak problems.

Also the field is bound to a data entity model.


Solution

  • If you don't want to use DatagridTemplateColumn then you can change DataGridTextColumn.EditingElementStyle and set TextBox.MaxLength there:

    <DataGridTextColumn Binding="{Binding Path=SellingPrice, UpdateSourceTrigger=PropertyChanged}">
       <DataGridTextColumn.EditingElementStyle>
          <Style TargetType="{x:Type TextBox}">
             <Setter Property="MaxLength" Value="10"/>
          </Style>
       </DataGridTextColumn.EditingElementStyle>
    </DataGridTextColumn>