I am attempting to set the field width programmatically and it's not working. I am also updating the Field position (the ActualPosition) and the SortedFields and that's working without issue.
Here is the code I'm using:
string[] fieldList = ViewModel.ColumnNameList.Split(','); string[] sizeList = ViewModel.ColumnSizeList.Split(','); List<SortColumn> sortColumns = ViewModel.GridSortColumns; List<SortColumn> groupByColumns = ViewModel.GridGroupbyColumns; FieldLayout currentFields = _xamDataGrid.FieldLayouts[0]; currentFields.SortedFields.Clear(); for (int i = 0; i < fieldList.Length; ++i) { if (currentFields.Fields.IndexOf(fieldList[i]) > -1) { int fieldIndex = currentFields.Fields[fieldList[i]].ActualPosition.Column; if (fieldIndex != i) { currentFields.Fields[fieldList[i]].ActualPosition = new FieldPosition(i, 0, 1, 1); currentFields.EnsureUniqueFieldPositions(); } if (i < sizeList.Length) { int columnSize = 0; if (int.TryParse(sizeList[i], out columnSize)) { currentFields.Fields[fieldList[i]].Width = new FieldLength(columnSize); // <= SETTING WIDTH HERE } } if (groupByColumns.Exists(p => p.ColumnName == fieldList[i])) { SortColumn sortColumn = groupByColumns.Find(p => p.ColumnName == fieldList[i]); FieldSortDescription currentSort = new FieldSortDescription(); currentSort.Direction = sortColumn.SortOrder == SortOrder.Ascending ? ListSortDirection.Ascending : ListSortDirection.Descending; currentSort.FieldName = fieldList[i]; currentSort.IsGroupBy = true; currentFields.SortedFields.Add(currentSort); } else if (sortColumns.Exists(p => p.ColumnName == fieldList[i])) { SortColumn sortColumn = sortColumns.Find(p => p.ColumnName == fieldList[i]); FieldSortDescription currentSort = new FieldSortDescription(); currentSort.Direction = sortColumn.SortOrder == SortOrder.Ascending ? ListSortDirection.Ascending : ListSortDirection.Descending; currentSort.FieldName = fieldList[i]; currentSort.IsGroupBy = false; currentFields.SortedFields.Add(currentSort); } } }
While debugging I noticed that the Width property of the field was not being updated when I resized the column in the grid (interactively, by dragging with the mouse).
Hi cthomson81,
Are you assigning the Width value to the same value it had previously? Try setting the width to null first before assigning your value. If the value you are trying to assign it is the same width as it was before, the column won't update. A property changed notification is probably not firing since the width values turned out to be the same. Setting it to null first should ensure that the width is not the same as it was before.
Yes this works. I set the Width to 0 first, then to the real value and it updates properly.