Setting DataGridColumn.DisplayIndex reverse of XAML causes bunched up columns

Apr 28, 2009 at 8:02 PM
Hi I think I've discovered a nifty bug with the DataGrid and the DataGridColumns.

Firstly I extended the standard DataGrid to have some drag and drop behaviour and named it DragDropDataGrid. I'm also using Tomer's WPF persistent state code to remember the column widths. I was also using it to remember column order but thought this bug was caused by Tomer's code. It wasn't.

I have the columns declared for my DataGrid in XAML like so:

                        <dg:DataGridTextColumn SortDirection="Descending" x:Name="date" ElementState.UId="CallHistoryDateColumn" ElementState.Mode="Persist" Width="{PropertyState Default=Auto}" Binding="{Binding Path=Info.TimeInitiated, Mode=OneWay}" Header="Date"/>
                        <dg:DataGridTemplateColumn x:Name="status" ElementState.UId="CallHistoryIconColumn" ElementState.Mode="Persist" Width="{PropertyState Default=Auto}"  Header="Status" SortMemberPath="Info.Type">
                                    <ContentControl x:Name="statusicon" Width="{DynamicResource IconPresenceStatus_Med}"  Height="{DynamicResource IconPresenceStatus_Med}" Template="{Binding Path=Info.Type, Converter={StaticResource CallTypeToIconConverter}}" VerticalAlignment="Top"/>
                        <dg:DataGridTextColumn x:Name="address" ElementState.UId="CallHistoryAddressColumn" ElementState.Mode="Persist" Width="{PropertyState Default=Auto}"  Binding="{Binding Path=Info.Display, Mode=OneWay}" Header="Address"/>

I have a Loaded event handler that changes the DisplayIndex to the last saved layout:

private void HistoryDataGrid_Loaded(object sender, RoutedEventArgs e)
         foreach (DataGridColumn col in this.HistoryDataGrid.Columns)
            string head = (string)col.Header;
            if (!col.Width.IsStar)
               col.Width = new DataGridLength(1.0, DataGridLengthUnitType.Star);
            if (head.Equals("Status"))
               col.DisplayIndex = Properties.Settings.Default.HistoryStatusIndex;
            if (head.Equals("Date"))
               col.DisplayIndex = Properties.Settings.Default.HistoryDateIndex;
            if (head.Equals("Address"))
               col.DisplayIndex = Properties.Settings.Default.HistoryAddressIndex;

The bug shows up when the saved order is the exact reverse of the XAML.
XAML has the columns declared, Date, Status, Address. If I restore the order to Address, Status, Date, the columns get all mashed up into one column only showing the header for address. In addition the Address header can still be dragged around as if the other columns were in fact there.

Apr 28, 2009 at 9:41 PM
This is a known bug. In the mean time, you can workaround the issue by doing the following:


DispatcherPriority.Background, (DispatcherOperationCallback)delegate(object args)




// Assign the DisplayIndex here



return null;





Apr 29, 2009 at 6:57 PM
Hi Ben,

Thanks for the code snippet. It works fine when my DataGrid is visible on app startup.  I'm having problems though when the DataGrid is in a hidden state. I tried using your code on IsVisibleChanged and it didn't seem to have any effect.
Dec 14, 2009 at 6:30 PM

Is there a timeline on a fix for this bug yet?

Dec 18, 2009 at 10:53 PM

I am using the source code release of the Toolkit for the datagrid, so if anyone knows what needs to be changed I can apply a patch and be on my merry way.

Dec 21, 2009 at 10:00 PM

My mistake. Updated the WPFToolkit source and the problem is solved.