Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have built a datagrid dynamicly, but found no way to set a MaxLength for a DataGridTextColumn. In Xaml it looks like this:

<DataGridTextColumn.EditingElementStyle>
	<Style TargetType="{x:Type TextBox}">
		<Setter Property="MaxLength" Value="10"/>
	</Style>
</DataGridTextColumn.EditingElementStyle>


What I have tried:

I tried this, but it does'nt work:

Style CellStyleLength = new Style(typeof(DataGridCell));
CellStyleLength.Setters.Add(new Setter(TextBox.MaxLengthProperty, 10));
DataGridTextColumn c1 = new DataGridTextColumn
                        {
                        Header = "Anrede",
                        Binding = new Binding("anrede"),
                        Width = 200,
                        CellStyle = CellStyleLength,
                        };
DgrShow.Columns.Add(c1);
Posted
Updated 13-Dec-22 19:13pm
v4

1 solution

Try this;

XML
<DataGridTextColumn.EditingElementStyle>
	<Style TargetType="TextBox">
		<Setter Property="MaxLength" Value="10"/>
	</Style>
</DataGridTextColumn.EditingElementStyle>
 
Share this answer
 
Comments
Member 12459153 14-Dec-22 7:12am    
Hi Mike, thanks for your answer. The Solution in Xaml ist not the problem. I want to create a DataGrid by code an implement a solution for the Cellstyle of textlength in my c# code.
Mike Hankey 14-Dec-22 9:39am    
Sorry mis-read your post, this might work.

Style _style = new Style(typeof(TextBox));
_style.Setters.Add(new Setter(TextBox.MaxLengthProperty, 10));

dg.AutoGenerateColumns=false;

dg.Columns.Add(new DataGridTextColumn() 

Header="Anrede",    
Binding=new Binding("Anrede"),
EditingElementStyle=_style

});
Member 12459153 14-Dec-22 11:25am    
Hi Mike, thanks a lot. That is the solution. I spent a lot of hours with this problem and found no info by googeling :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900