Data Grid Row Header Not Resizing To Contents

463 Views Asked by At

I'm customizing a DataGrid so that the user can enter information directly into the header via a TextBox.

The problem that I am having is that the row headers are not resizing to match the size of the contents when the text changes:

Before: enter image description here

After: enter image description here enter image description here

As you can see, the header does not reduce in size to match the text boxes once the size of the text box has been reduced to match the new text.

In compliance with the Minimal, Complete and Verifiable Example Requirements:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:l="clr-namespace:MCVE"
    xmlns:lib="clr-namespace:System;assembly=mscorlib"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Class="MCVE.MainWindow"
    mc:Ignorable="d" Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <x:Array x:Key="Source" Type="{x:Type lib:String}">
            <lib:String>Foo</lib:String>
            <lib:String>Bar</lib:String>
            <lib:String>Baz</lib:String>
        </x:Array>
    </Window.Resources>
    <DataGrid
        AutoGenerateColumns="False"
        ItemsSource="{StaticResource Source}"
        RowHeight="50">
        <DataGrid.RowHeaderStyle>
            <Style TargetType="{x:Type DataGridRowHeader}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <TextBox FontSize="36" HorizontalAlignment="Left" Text="This Is A Test" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.RowHeaderStyle>
    </DataGrid>
</Window>

Type some stuff into any of the row headers. Then clear it to reproduce.

So what can I do to enforce the row header width and make certain that it maintains the smallest width possible ( without encroaching on the actual row header contents )?

1

There are 1 best solutions below

5
On BEST ANSWER

You could handle the SizeChanged event for the TextBox elements and keep track of their width's. Try this:

private readonly Dictionary<TextBox, double> _widths = new Dictionary<TextBox, double>();
private void TextBox_SizeChanged(object sender, SizeChangedEventArgs e)
{
    TextBox textBox = (TextBox)sender;
    _widths[textBox] = textBox.ActualWidth;

    double largestWidth = _widths.Values.Max();
    DataGridRowHeader header = FindParent<DataGridRowHeader>(textBox);
    dg.RowHeaderWidth = double.NaN;
    if (header != null)
        dg.RowHeaderWidth = dg.RowHeaderActualWidth > largestWidth ? largestWidth : double.NaN;
}

XAML:

<DataGrid.RowHeaderStyle>
    <Style TargetType="{x:Type DataGridRowHeader}">
        <Setter Property="Width" Value="Auto" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <TextBox FontSize="36" HorizontalAlignment="Left" Text="This Is A Test"
                                           SizeChanged="TextBox_SizeChanged" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGrid.RowHeaderStyle>