set text wrapping for an autogenerated datagrid textbox by setting a behavior

Well, after fiddling around with this problem for several hours I decided to resolve the issue by replacing the column entirely.

private void OnGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    if (e.PropertyDescriptor is PropertyDescriptor descriptor)
        e.Column.Header = descriptor.DisplayName ?? descriptor.Name;
        if (descriptor.DisplayName == "Description")
            var column = new DataGridTemplateColumn();
            var element = new FrameworkElementFactory(typeof(TextBlock));
            var binding = new Binding("Description");
            var template = new DataTemplate();

            element.SetBinding(TextBlock.TextProperty, binding);
            element.SetValue(TextBox.TextWrappingProperty, TextWrapping.Wrap);
            template.VisualTree = element;
            column.CellTemplate = template;
            column.Header = e.Column.Header;
            column.Width = 300;
            e.Column = column;
        e.Cancel = true;

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top