/// <summary>
        /// Creates the report
        /// </summary>
        /// <returns></returns>
        private async Task CreateReport()
        {
            ReportDataSource reportDataSource = null;
            Report           report           = null;

            _isCreateReport = false;
            var reportTemplate = await GetReportTemplate(SelectedReportTemplate);

            await QueuedTask.Run(() =>
            {
                //Adding all fields to the report
                List <CIMReportField> reportFields = new List <CIMReportField>();
                var selectedReportFields           = ReportFields.Where((fld) => fld.IsSelected);
                foreach (var rfld in selectedReportFields)
                {
                    var cimReportField = new CIMReportField()
                    {
                        Name = rfld.Name
                    };
                    reportFields.Add(cimReportField);
                    //Defining grouping field
                    if (rfld.Name == SelectedGroupField?.Name)
                    {
                        cimReportField.Group = true;
                    }
                    //To DO: Do sort info here.
                }

                //Report field statistics
                List <ReportFieldStatistic> reportFieldStats = new List <ReportFieldStatistic>();

                if (SelectedStatsField != null)
                {
                    ReportFieldStatistic reportStat = new ReportFieldStatistic();
                    reportStat.Field     = SelectedStatsField.Name;
                    reportStat.Statistic = (FieldStatisticsFlag)Enum.Parse(typeof(FieldStatisticsFlag), SelectedStatsOption);
                    reportFieldStats.Add(reportStat);
                }

                //Set Datasource
                reportDataSource = new ReportDataSource(SelectedLayer, "", IsUseSelection, reportFields);

                try
                {
                    report          = ReportFactory.Instance.CreateReport(ReportName, reportDataSource, null, reportFieldStats, reportTemplate, SelectedReportStyle);
                    _isCreateReport = true;
                }
                catch (System.InvalidOperationException e)
                {
                    if (e.Message.Contains("Group field defined for a non-grouping template"))
                    {
                        MessageBox.Show("A group field cannot be defined for a non-grouping template.");
                    }
                    else if (e.Message.Contains("Grouping template specified but no group field defined"))
                    {
                        MessageBox.Show("A group field should be defined for a grouping template.");
                    }
                }
            });
        }
    private bool CanUpdateReport()
		{
			var canUpdateReport = false;
			if (_reportsInProject.Any(r => r.Name == ReportName))
				canUpdateReport = true;
			//Check if there are fields selected in the UI not in the report. If yes, then we need to update.
			var reportToUpdate = _reportsInProject.FirstOrDefault(r => r.Name == ReportName);
			if (reportToUpdate == null) return false;
			var cimFieldsInReport = reportToUpdate.DataSource.Fields.ToList();
			//This is a "ReportField" collection. Change to CIMReportField collection
			var reportFieldsSelected = SelectedFields.ToList();
			List<CIMReportField> cimReportFieldsSelected = new List<CIMReportField>();
			foreach (var rfld in reportFieldsSelected)
			{
				var cimReportField = new CIMReportField() { Name = rfld.Name };
				cimReportFieldsSelected.Add(cimReportField);
			}
			//New fields: Fields in the "SelectedFields" of dockpane, but not in the report object.
			_fieldsAddToReport = cimReportFieldsSelected.Except(cimFieldsInReport, new ReportFieldComparer()).ToList();

			canUpdateReport = _fieldsAddToReport.Count == 0 ? false : true;

			//TODO: Check if a new statistics is checked
			//If nothing new, no need to update

			return canUpdateReport;
		}
        private static void CreateField(Report report)
        {
            #region Create a new field in the report
            //This is the gap between two fields.
            double fieldIncrement = 0.9388875113593206276389;
            //On the QueuedTask
            //New field to add.
            var newReportField = new CIMReportField
            {
                Name       = "POP1990",
                FieldOrder = 2,
            };
            //Get the "ReportSection element"
            var mainReportSection = report.Elements.OfType <ReportSection>().FirstOrDefault();
            if (mainReportSection == null)
            {
                return;
            }

            //Get the "ReportDetails" within the ReportSectionElement. ReportDetails is where "fields" are.
            var reportDetailsSection = mainReportSection?.Elements.OfType <ReportDetails>().FirstOrDefault();
            if (reportDetailsSection == null)
            {
                return;
            }

            //Within ReportDetails find the envelope that encloses a field.
            //We get the first CIMParagraphTextGraphic in the collection so that we can add the new field next to it.
            var lastFieldGraphic = reportDetailsSection.Elements.FirstOrDefault((r) =>
            {
                var gr = r as GraphicElement;
                if (gr == null)
                {
                    return(false);
                }
                return(gr.GetGraphic() is CIMParagraphTextGraphic ? true : false);
            });
            //Get the Envelope of the last field
            var graphicBounds = lastFieldGraphic.GetBounds();

            //Min and Max values of the envelope
            var xMinOfFieldEnvelope = graphicBounds.XMin;
            var yMinOfFieldEnvelope = graphicBounds.YMin;

            var xMaxOfFieldEnvelope = graphicBounds.XMax;
            var YMaxOfFieldEnvelope = graphicBounds.YMax;
            //create the new Envelope to be offset from the existing field
            MapPoint newMinPoint      = MapPointBuilder.CreateMapPoint(xMinOfFieldEnvelope + fieldIncrement, yMinOfFieldEnvelope);
            MapPoint newMaxPoint      = MapPointBuilder.CreateMapPoint(xMaxOfFieldEnvelope + fieldIncrement, YMaxOfFieldEnvelope);
            Envelope newFieldEnvelope = EnvelopeBuilder.CreateEnvelope(newMinPoint, newMaxPoint);

            //Create field
            GraphicElement fieldGraphic = ReportElementFactory.Instance.CreateFieldValueTextElement(reportDetailsSection, newFieldEnvelope, newReportField);
            #endregion
        }