Here is our problem...
We have a custom control for adding and removing columns to our grids. When we update the grid, we have to rebuild the headers. It doesn't work in all cases. If the user has moved the splitter, the multi-level structure collapses and the columns appear in a random order. But only if the splitter has been moved.
I have built a simple test project to highlight the core problem for us. The data comes from a published Federal report on border crossing activity.
Steps to see the error:
Does any one have a fix or work-around for this issue?
Thanks!
Anyone?
Hmm...
How do I raise this to bug status?
The following workaround was provided by Andrew Smith:
When you dragged the splitter bar you created a customization for the field positions just as would have happened if field moving was allowed and you moved the fields. When they cleared the fields and populated with new ones that means the fields that were associated with the customization are no longer in the fields collection and so we end up removing the position information for the removed field instances and then adding the newly added field instances into the existing position information as if a new field had been added to the datasource. When that happens we have to ignore the field position information since that could overlap with an existing field and so the fields are just appended to the layout.
If the intent is to lose the fixed information (after all since you are creating new field’s you are losing state on those fields like the FixedPosition and you would lose other stuff like summaries, etc.), then you should probably call ClearCustomizations before clearing the fields collection.
private static void Fields_Changed(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var grid = obj as XamDataGrid;
var fieldCollection = e.NewValue as List<Field>;
if (grid == null || fieldCollection == null)
return;
grid.ClearCustomizations( CustomizationType.FieldPosition );
if (grid.FieldLayouts == null || grid.FieldLayouts.Count == 0)
grid.FieldLayouts.Add(new FieldLayout());
}
else
grid.FieldLayouts[0].Fields.Clear();
fieldCollection.ForEach(grid.FieldLayouts[0].Fields.Add);
If the intent is to keep the customizations (e.g. the fixed position) then you should save the customizations, clear them before manipulating the fields and then load the customizations.
E.g.
string customizations = grid.SaveCustomizations();
grid.ClearCustomizations( CustomizationType.All );
grid.LoadCustomizations(customizations);
Note, in order for this to work you will need to ensure that you set the Name of all the fields you create. I noticed in the MainViewModel’s BuildHeaders method you don’t assign a Name to some of the “header” fields nor is there a binding or anything else that could be used to uniquely identify the fields in the customization file.