如何将WPF DataGridTextColum Text最大长度限制为10个字符

时间:2022-12-23 22:18:28

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

如何将WPF DataGridTextColumn文本限制为最多10个字符的长度。

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

我不想使用DatagridTemplateColumn,因为它有内存泄漏问题。

Also the field is bound to a data entity model.

该字段也绑定到数据实体模型。

1 个解决方案

#1


10  

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

如果您不想使用DatagridTemplateColumn,则可以更改DataGridTextColumn.EditingElementStyle并在那里设置TextBox.MaxLength:

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

#1


10  

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

如果您不想使用DatagridTemplateColumn,则可以更改DataGridTextColumn.EditingElementStyle并在那里设置TextBox.MaxLength:

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