Resizing Issues with DataGrid containing wrapping TextBlocks

I have a really disturbing issue with .NET 4.0 DataGrid. I have proportional template columns containing TextBlock with textWrapping enabled.

Problem is, the height of the DataGrid is not correct at load time (it's sized as if the textblock were all wrapped at their maximum.) and does not update their size when resizing. It appears to be a layout issue (MeasureOverride and ArrangeOverride seems to be called when proportional sizes are not resolved, and not called afterwards...) but I haven't been able to solve it.

Here is a simplified code that shows the issue :


<Window x:Class="WpfApplication1.MainWindow"
    Title="MainWindow" Height="700" Width="525">
<StackPanel Width="500">
    <Button Content="Add DataGrid" Click="Button_Click" />
    <ItemsControl x:Name="itemsControl">
                <Setter Property="Control.Margin" Value="5" />


using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace WpfApplication1
public partial class MainWindow : System.Windows.Window
    public MainWindow()

    private void Button_Click(object sender, RoutedEventArgs e)

    private DataGrid CreateDataGrid()
        var dataGrid = new DataGrid() { HeadersVisibility = DataGridHeadersVisibility.Column };
        dataGrid.MaxWidth = 500;
        dataGrid.Background = Brushes.LightSteelBlue;
        dataGrid.Columns.Add(GetDataGridTemplateColumn("Label", "Auto"));
        dataGrid.Columns.Add(GetDataGridTemplateColumn("Value", "*"));
        dataGrid.Items.Add(new Entry() { Label = "Text Example 1", Value = "Some wrapped text" });
        dataGrid.Items.Add(new Entry() { Label = "Text Example 2", Value = "Some wrapped text" });
        return dataGrid;

    private DataGridTemplateColumn GetDataGridTemplateColumn(string bindingPath, string columnWidth)
        DataGridTemplateColumn result = new DataGridTemplateColumn() { Width = (DataGridLength)(converter.ConvertFrom(columnWidth))};
        FrameworkElementFactory cellTemplateframeworkElementFactory = new FrameworkElementFactory(typeof(TextBox));
        cellTemplateframeworkElementFactory.SetValue(TextBox.NameProperty, "cellContentControl");
        cellTemplateframeworkElementFactory.SetValue(TextBox.TextProperty, new Binding(bindingPath));
        cellTemplateframeworkElementFactory.SetValue(TextBox.TextWrappingProperty, TextWrapping.Wrap);
        result.CellTemplate = new DataTemplate() { VisualTree = cellTemplateframeworkElementFactory };
        return result;

    private static DataGridLengthConverter converter = new DataGridLengthConverter();

public class Entry
    public string Label { get; set; }
    public string Value { get; set; }



I finally came up with a rather dirty solution, but at least it works : I'm manually updating the Height of the grid by computing myself the right value, taking into account Rows height, Grid Padding, Grid border and ColumnHeaderRow height. I need to update it on OnPropertyChanged (for padding and grid border), DataGridColumn.SizeChanged, DataGridColumn.RowUnloaded, ColumnHeaderPresenter.SizeChanged, and a few others.

Only issue is that it works correctly with the default DataGrid ControlTemplate but would not be correct anymore if a template was to change the grid render.

Finally found a solution : setting CanContentScroll to false on the DataGrid fixed the issue.

<Setter Property="ScrollViewer.CanContentScroll" Value="False" />

Need Your Help

Winform MenuStrip will not change from default color

c# winforms colors .net-4.5

In my application, I've been trying to change the background color of the MenuStrip, but nothing seems to work. I've tried to change its background just like changing the other component's background

Displaying objects based on if a user is logged in or not

php html session

I'm learning about PHP sessions for user authentication on my website. I know how to restrict the viewing of a complete page using sessions (simply check if the 'uid' session variable is set and if...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.