Search code examples
wpfwpfdatagridstylingresizedatagridrowheader

How to support re-sizing of DataGridRowHeader column in DataGrid


I would like to allow re-sizing of zeroth column of dataGrid, the column which contains the SelectAll button and row headers -

enter image description here

Image taken from - WPF Toolkit DataGrid, Part II – Custom styling


Solution

  • Try this...

    XAML:

       <tk:DataGrid x:Name="MyDataGrid"
                    RowHeaderWidth="15"
                    ItemsSource="{StaticResource MyData}"
                    AutoGenerateColumns="False">
            <tk:DataGrid.Columns>
                <tk:DataGridTextColumn Binding="{Binding ID}">
                    <tk:DataGridTextColumn.HeaderTemplate>
                        <DataTemplate>
                            <DockPanel>
                                <GridSplitter
                                    DockPanel.Dock="Left"
                                    VerticalAlignment="Stretch"
                                    HorizontalAlignment="Left" Width="2"
                                    DragDelta="GridSplitter_DragDelta"/>
                                <TextBlock Text="ID" DockPanel.Dock="Right"/>
                            </DockPanel>
                        </DataTemplate>
                    </tk:DataGridTextColumn.HeaderTemplate>
                </tk:DataGridTextColumn>
                <tk:DataGridTextColumn Header="Name"
                                       Binding="{Binding Name}">
                </tk:DataGridTextColumn>
            </tk:DataGrid.Columns>
        </tk:DataGrid>
    

    Code Behind:

         private void GridSplitter_DragDelta
            (object sender,
             System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            if (MyDataGrid.RowHeaderWidth + e.HorizontalChange >= 15)
            {
                MyDataGrid.RowHeaderWidth = MyDataGrid.RowHeaderWidth + e.HorizontalChange;
            }
        }
    

    You could override RowHeaderTemplate and do this exact grid splitter docked to right hand side. But for me docking it to left on the first column header worked the best!