views:

4173

answers:

0

I have a DataGrid defined as

<wpftoolkit:DataGrid
        x:Name="AccountsDataGrid"
        AutoGenerateColumns="False"
        ItemsSource="{Binding Path=Accounts}"
        ColumnReordered="DataGrid_ColumnReordered"
        SelectionUnit="FullRow"
        RowHeaderWidth="0"
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        >
        <wpftoolkit:DataGrid.Columns>
            <wpftoolkit:DataGridTextColumn Header="Account Id" Binding="{Binding Path=AccountId}" Width="Auto" />
            <wpftoolkit:DataGridTextColumn Header="Account Name" Binding="{Binding Path=AccountName}" Width="*" />
        </wpftoolkit:DataGrid.Columns>
    </wpftoolkit:DataGrid>

which on load looks great. the first column fits to the minimum width necessary to fit both the content and the header. the second column stretches to fill the rest of the width of the DataGrid (so I don't have a 3rd filler column). But if I try to reorder the columns, the AccountName column cannot be resized to a width less than what its width was before the reorder. So I added a handler on the ColumnReordered event, figuring I can just reset the column widths, but it doesn't seem to work. In fact, it shrinks the AccountId column to almost nothing, and the AccountName column still cannot be resized smaller.

private void DataGrid_ColumnReordered(object sender, Microsoft.Windows.Controls.DataGridColumnEventArgs e)
    {
        foreach (DataGridColumn column in AccountsDataGrid.Columns)
        {
            if (column.Equals(AccountsDataGrid.Columns.Last()))
            {
                column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Star);
            }
            else
            {
                column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Auto);
            }
        }
    }

Is there a better way to handle the column widths and why does the column reordering break the column resizing ability