public void BuildRowStructure(double available_width)
        {
            FormRowData new_row = new FormRowData();
            double      width   = 0d;

            for (int i = 0; i < _fields.Length; i++)
            {
                FormFieldData field = _fields[i];

                if (field == null) // This is a NewLine.
                {
                    if (new_row.Fields.Count > 0)
                    {
                        _rows.Add(new_row);
                    }

                    new_row = new FormRowData();
                    width   = 0d;
                }
                else // field is not null
                {
                    if ((width + field.Width) > available_width) // wrap the formfield to a new line.
                    {
                        if (new_row.Fields.Count > 0)
                        {
                            _rows.Add(new_row);
                        }

                        new_row = new FormRowData();
                        width   = 0;
                    }

                    new_row.Fields.Add(field);
                    width += field.Width;
                }
            }

            if (new_row.Fields.Count > 0)
            {
                _rows.Add(new_row);
            }
        }
        private void InitializeFieldDataObjects()
        {
            for (int i = 0; i < this.Children.Count; i++)
            {
                FrameworkElement element = this.Children[i] as FrameworkElement;

                if (element == null)
                {
                    throw new Exception("a Form child is not a FrameworkElement");
                }

                if (element.ReadLocalValue(FrameworkElement.WidthProperty) != DependencyProperty.UnsetValue)
                {
                    throw new Exception("Do not set the Width property for Form children.");
                }

                if (element is FormField)
                {
                    double fieldwidth = ((FormField)element).FieldWidth;

                    // Measure the element
                    element.Measure(new Size(fieldwidth, double.PositiveInfinity));
                    var desired = element.DesiredSize;

                    _fields[i] = new FormFieldData
                    {
                        Width  = fieldwidth,
                        Height = desired.Height
                    };
                }
                else if (element is NewLine)
                {
                    _fields[i] = null;
                }
                else
                {
                    throw new Exception("unknown element type");
                }
            }
        }