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
15
Adding FieldGroups to FieldItems list
posted

Hello there, in our project we need to add FieldGroups into the FieldItems list of the FieldLayout. We need to do this from VM, since our view component is a generic one, meant to be reused. 
Our problem is that adding the FieldGroups into the FieldItems links to unexpected behavior, like columns with bad arrangement, index out of range exception.
The code from below is from a bigger method, but this part is responsible for adding FieldGroups to FieldItems collection. 
(It works, but not as good as we hoped.)

if (!string.IsNullOrEmpty(column.Group?.Id))
			{
				if (!groups.TryGetValue(column.Group.Id, out var group))
				{
					group = new FieldGroup
					{
						Label = column.Group.Label,
						AllowHiding = column.Group.AllowUserHiding ? AllowFieldHiding.ViaFieldChooserOnly : AllowFieldHiding.Never,
						Visibility = column.Group.Visibility,
						LabelTextAlignment = column.Group.LabelAlignment
					};

					groups[column.Group.Id] = group;
				}

				group.Children.Add(field);
			}
			else
				fieldLayout.FieldItems.Add(field);
		}

		foreach (var group in groups.Values)
			if (!fieldLayout.FieldItems.Contains(group))
				fieldLayout.FieldItems.Add(group);