Click here to Skip to main content
15,944,136 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:

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

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,
Updated 13-Dec-22 19:13pm

1 solution

Try this;

	<Style TargetType="TextBox">
		<Setter Property="MaxLength" Value="10"/>
Share this answer
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.Columns.Add(new DataGridTextColumn() 

Binding=new Binding("Anrede"),

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