How can I set the width of a column after my grid has already been loaded? (seems like such a simple task but I can't seem to get it...)Example: Program is running, user performs some action, I want to resize one or more columns.
Thank you,Mike
Rather than using an event I have synchronised the widths of the columns of a Master and Details grid by binding the Details grid Field Width to the CellResolvedWidth of the corresponding Master Field:
foreach(var field in _myDataGrid.FieldLayouts[1].Fields){ var b = new Binding("CellWidthResolved"); b.Source = _myDataGrid.FieldLayouts[0].Fields[fieldName]; BindingOperations.SetBinding(field, Field.WidthProperty, b);
}
This seems to work nicely.
Actually you wouldn't want to do that since you would essentially be telling the grid to ignore the size information that was just set, and AutoSize to the contents as if the end user double-clicked on the edge of a column.
Joe
Apparently, you may need to call fieldServant.PerformAutoSize() after setting width to actually change it.
Hi Ron -
Looking at your sample code, I think the problem is that you are using the CellWidth and LabelWidth properties on the Settings object as your source. The grid does not actually set these properties when the end user changes the width of a column - the properties on the Settings object are typically set by the developer to control behavior/appearance. You should instead be using the CellWidthResolved and LabelWidthResolved properties on the Field as the source like so:
fieldServant.Settings.CellWidth = fieldMaster.CellWidthResolved ;
fieldServant.Settings.LabelWidth = fieldMaster.LabelWidthResolved ;
Even better would be to change the target of the property setting to use the Field.Width property which is a little more efficient since a Settings object is not created and also more convenient since you don't have to set both Label AND Cell widths:
fieldServant.Width = new FieldLength(fieldMaster.CellWidthResolved, FieldLengthUnitType.Pixel);
Looks like it is still not fixed in 10.1