private void RenderTemplate()
        {
            Stack.Children.Clear();

            DisableModifiedTracking();

            var hasEmptySection = false;

            foreach (var section in Sections)
            {
                section.Control = new UI.TemplateDesigner.TemplateSection();
                section.Control.Initialize(this, section);

                Stack.Children.Add(section.Control);

                if (string.IsNullOrEmpty(section.Name))
                {
                    hasEmptySection = true;
                }

                var hasEmptyField = false;

                foreach (var field in section.Fields)
                {
                    var fieldControl = new UI.TemplateDesigner.TemplateField();
                    field.Control = fieldControl;
                    field.Section = section;

                    fieldControl.Initialize(this, field);

                    section.Control.FieldStack.Children.Add(fieldControl);

                    if (string.IsNullOrEmpty(field.Name))
                    {
                        hasEmptyField = true;
                    }
                }

                if (!hasEmptyField)
                {
                    section.Control.CreateEmpyField();
                }
            }

            if (!hasEmptySection)
            {
                var newSection = new TemplateSection();
                Sections.Add(newSection);
                newSection.Id = Guid.NewGuid().ToString(@"B").ToUpperInvariant();

                newSection.Control = new UI.TemplateDesigner.TemplateSection();
                newSection.Control.Initialize(this, newSection);

                Stack.Children.Add(newSection.Control);
            }

            EnableModifiedTracking();
        }
        private void DropTemplateField([NotNull] string response, [NotNull] ExecuteResult executeresult)
        {
            Debug.ArgumentNotNull(response, nameof(response));
            Debug.ArgumentNotNull(executeresult, nameof(executeresult));

            if (!DataService.HandleExecute(response, executeresult))
            {
                return;
            }

            XDocument doc;

            try
            {
                doc = XDocument.Parse(response);
            }
            catch
            {
                return;
            }

            var field = doc.Root;

            if (field == null)
            {
                return;
            }

            var section = Sections.Last();

            if (section == null)
            {
                section = new TemplateSection();
                Sections.Add(section);
            }

            if (string.IsNullOrEmpty(section.Name))
            {
                section.Name = @"Data";
            }

            if (section.Fields.Any())
            {
                section.Fields.RemoveAt(section.Fields.Count - 1);
            }

            LoadTemplateFields(field, section);

            RenderTemplate();
            SetModified(true);
        }
        private void LoadTemplateSections([NotNull] XElement template)
        {
            Debug.ArgumentNotNull(template, nameof(template));

            foreach (var sectionElement in template.Elements())
            {
                var templateSection = new TemplateSection
                {
                    Id   = sectionElement.GetAttributeValue("id"),
                    Name = sectionElement.GetAttributeValue("name")
                };

                Sections.Add(templateSection);

                LoadTemplateFields(sectionElement, templateSection);
            }
        }
        private void LoadTemplateFields([NotNull] XElement sectionElement, [NotNull] TemplateSection templateSection)
        {
            Debug.ArgumentNotNull(sectionElement, nameof(sectionElement));
            Debug.ArgumentNotNull(templateSection, nameof(templateSection));

            foreach (var fieldElement in sectionElement.Elements())
            {
                var templateField = new TemplateField
                {
                    Id          = fieldElement.GetAttributeValue("id"),
                    Name        = fieldElement.GetAttributeValue("name"),
                    Type        = fieldElement.GetAttributeValue("type"),
                    Source      = fieldElement.GetAttributeValue("source"),
                    Shared      = fieldElement.GetAttributeValue("shared") == @"1",
                    Unversioned = fieldElement.GetAttributeValue("unversioned") == @"1",
                    Validations = fieldElement.GetAttributeValue("validatorbar")
                };

                templateSection.Fields.Add(templateField);
            }
        }