Esempio n. 1
0
        /// <summary>For the header/footer</summary>
        public static BindingContextPart CreateHeaderOrFooterBindingContextPart(IBindingContext parent, ITemplateDefinitionPart templateDefinitionPart, BindingContextPartType partType, object dataSource)
        {
            BindingContextPart ret = new BindingContextPart(parent, templateDefinitionPart, partType);

            ret.Elements         = new BindingContextElement[] { new BindingContextElement(ret, dataSource, 0) };
            ret.ElementsToRender = ret.Elements.ToList();
            return(ret);
        }
Esempio n. 2
0
        public static BindingContextPart CreateBodyBindingContextPart(BindingContext parent, ITemplateDefinitionPart templateDefinitionPart, List <object> dataSourceAsList, ISortersAndFilters externalSorterAndFilter, ISortersAndFilters templatedSorterAndFilter)
        {
            BindingContextPart ret = new BindingContextPart(parent, templateDefinitionPart, BindingContextPartType.Body);

            ret.ExternalSorterAndFilter = parent.ExternalSortsAndFilters;

            int elementIndex = 0;
            // Bug => Closure !!!!
            // ret.Elements = dataSourceAsList.Select(ds => new BindingContextElement(ret, ds, elementIndex++));
            List <IBindingContextElement> tmpElements = new List <IBindingContextElement>();

            foreach (object obj in dataSourceAsList)
            {
                tmpElements.Add(new BindingContextElement(ret, obj, elementIndex++));
            }
            ret.Elements = tmpElements;

            // The sorterers and filters defined outside templates have the priority
            if (parent.ExternalSortsAndFilters != null)
            {
                Dictionary <object, IBindingContextElement> contextItemByElement = ret.Elements.ToDictionary(e => e.Element, e => e);
                IEnumerable <object> elements = parent.ExternalSortsAndFilters.Execute(contextItemByElement.Keys) as IEnumerable <object>;
                ret.ElementsToRender = elements.Select(e => { IBindingContextElement el = null;
                                                              contextItemByElement.TryGetValue(e, out el);
                                                              return(el); });
            }
            else
            {
                ret.ElementsToRender = ret.Elements;
            }

            // Manage the filters defined in the templates
            if (templatedSorterAndFilter != null)
            {
                Dictionary <object, IBindingContextElement> contextItemByElement = ret.ElementsToRender.ToDictionary(e => e.Element, e => e);
                IEnumerable <object> elements = templatedSorterAndFilter.Execute(contextItemByElement.Keys) as IEnumerable <object>;
                ret.ElementsToRender = elements.Select(e => { IBindingContextElement el = null;
                                                              contextItemByElement.TryGetValue(e, out el);
                                                              return(el); });
            }
            return(ret);
        }
Esempio n. 3
0
        public BindingContext(IBindingContextElement parent, ITemplateView owner, ITemplateDefinition templateDefinition, object dataSource, List <IFilterDefinition> templatedFilters)
        {
            try
            {
                if (owner == null)
                {
                    throw new ArgumentNullException("The parameter 'owner' cannot be null");
                }
                if (templateDefinition == null)
                {
                    throw new ArgumentNullException("The parameter 'templateDefinition' cannot be null");
                }

                Owner = owner;
                TemplateDefinition = templateDefinition;
                TemplatedFilters   = templatedFilters;
                IsExpanded         = TemplateDefinition.TemplateOption.HeaderAsExpander != HeaderAsExpander.StartClosed;

                //TemplatedSortsAndFilters = templatedSortsAndFilters;
                Parent     = parent;
                DataSource = dataSource;

                if (DataSource != null)
                {
                    List <object>      dataSourceAsList;
                    IBindingDefinition dataSourceType;
                    if (DataSource is IEnumerable)
                    {
                        dataSourceAsList = (DataSource as IEnumerable).Cast <object>().ToList();
                        dataSourceType   = BindingDefinitionRoot.CreateInstance(dataSourceAsList.GetType());
                    }
                    else
                    {
                        dataSourceAsList = new List <object>();
                        dataSourceAsList.Add(DataSource); //new object[] { DataSource };
                        dataSourceType = BindingDefinitionRoot.CreateInstance(DataSource.GetType());
                    }

                    if (TemplateDefinition.MainBindingDefinition != null)
                    {
                        CheckType(TemplateDefinition.MainBindingDefinition, dataSourceType);
                    }

                    ISortersAndFilters externalSortersAndFilters = null;
                    owner.ExternalSortersAndFilters?.TryGetValue(TemplateDefinition, out externalSortersAndFilters);

                    //Occurrences = dataSourceAsList.Count;
                    if (TemplateDefinition.Body != null)
                    {
                        IEnumerable <IFilterDefinition> templatedFiltersToTakeIntoAccount = null;
                        if (templatedFilters != null)
                        {
                            IEnumerable <IFilterDefinition> templatedFiltersToTakeIntoAccountFound = templatedFilters.Where(tf => tf.TemplateDefinition == templateDefinition);
                            if (templatedFiltersToTakeIntoAccountFound.Any())
                            {
                                templatedFiltersToTakeIntoAccount = templatedFiltersToTakeIntoAccountFound;
                            }
                        }

                        ISorterDefinition[] sortersDefinition = null;
                        if (((TemplateView)owner).SorterDefinition != null && ((TemplateView)owner).SorterDefinition.TemplateDefinition == templateDefinition)
                        {
                            sortersDefinition = new ISorterDefinition[] { ((TemplateView)owner).SorterDefinition }
                        }
                        ;

                        ISortersAndFilters sortersAndFilters = null;
                        if (templatedFilters != null || sortersDefinition != null)
                        {
                            sortersAndFilters = SortersAndFilterersFactory.CreateInstance(templateDefinition, templatedFiltersToTakeIntoAccount, sortersDefinition);
                        }
                        Body = BindingContextPart.CreateBodyBindingContextPart(this, TemplateDefinition.Body, dataSourceAsList, externalSortersAndFilters, sortersAndFilters);
                    }

                    if (TemplateDefinition.Header != null)
                    {
                        Header = BindingContextPart.CreateHeaderOrFooterBindingContextPart(this, TemplateDefinition.Header, BindingContextPartType.Header, DataSource);
                    }
                    if (TemplateDefinition.Footer != null)
                    {
                        Footer = BindingContextPart.CreateHeaderOrFooterBindingContextPart(this, TemplateDefinition.Footer, BindingContextPartType.Header, DataSource);
                    }
                }
            }
            catch (Exception ex)
            {
                string message = $"Create the 'BindingContext' for template '{(templateDefinition == null ? string.Empty : templateDefinition.Name)}' failed . {ex.Message}";
                throw new EtkException(message);
            }
        }