Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
225
Field width changes being ignored
posted

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).