How do I set the MaxLength property of the DataGridTextColumn?
DataGridTextColumn.MaxLength?
12.7k views Asked by Shimmy Weitzhandler At
3
There are 3 answers
0
On
<Window.Resources>
<Style x:Key="sty_txtDesc" TargetType="TextBox">
<Setter Property="MaxLength" Value="495" />
</Style>
</Window.Resources>
for (int i = 0; i < miDataGridX.Columns.Count; i++)
{
if (miDataGridX.Columns[i].Header.ToString() == "Description")
{
((DataGridTextColumn)miDataGridX.Columns[i]).EditingElementStyle = (Style)this.FindResource("sty_txtDesc");
}
}
0
On
If you have a shared style among all columns, and you would like to add an additional style to one or more of those, you could use the Style.BasedOn Property:
<DataGridTextColumn Binding="{Binding SomeProperty, UpdateSourceTrigger=PropertyChanged}" ElementStyle="{StaticResource CellErrorStyle}">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox" BasedOn="{StaticResource OriginalStyleKey}">
<Setter Property="MaxLength" Value="5" />
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
You could also set it using the following behavior so you don't have to use a style and setters each time:
Usage: