Exemple #1
0
        double GetHeight(Dictionary <System.Windows.DataTemplate, FrameworkElement> reusables, System.Windows.DataTemplate template, object bindingContext)
        {
            double width = Control.ActualWidth;

            FrameworkElement content;

            if (!reusables.TryGetValue(template, out content))
            {
                content = (FrameworkElement)template.LoadContent();

                // Windows Phone refuses to properly bind things on a first pass or even a second pass unless it has different content
                // so we'll force it to cycle here the first time for each template.
                content.DataContext = bindingContext;
                content.Measure(new System.Windows.Size(width, double.PositiveInfinity));
                content.DataContext = null;
                content.Measure(new System.Windows.Size(width, double.PositiveInfinity));

                var control = content as Control;
                if (control != null)
                {
                    // Since we're not adding to the visual tree, we need to inherit the font to measure correctly.
                    control.FontFamily = Control.FontFamily;
                }

                reusables[template] = content;
            }

            content.DataContext = bindingContext;
            content.Measure(new System.Windows.Size(width, double.PositiveInfinity));
            return(content.DesiredSize.Height);
        }
Exemple #2
0
        public static FrameworkContentElement LoadDataTemplate(DataTemplate dataTemplate)
        {
            var content = dataTemplate.LoadContent();
            if (content is Fragment)
            {
                return ((Fragment)content).Content;
            }
            else if (content is TextBlock)
            {
                var inlines = ((TextBlock)content).Inlines;
                if (inlines.Count == 1)
                    return inlines.FirstInline;
                else
                {
                    var paragraph = new Paragraph();
                    // we can't use an enumerator, since adding an inline removes it from its collection
                    while (inlines.FirstInline != null)
                        paragraph.Inlines.Add(inlines.FirstInline);

                    return paragraph;
                }
            }
            else
            {
                throw new Exception("Data template needs to contain a <Fragment> or <TextBlock>");
            }
        }
Exemple #3
0
        /// <summary>
        /// Loads content in the specified template.
        /// </summary>
        /// <param name="template">Template to load.</param>
        /// <returns>Contents loaded from the specified template.</returns>
        public static DependencyObject LoadContent(DataTemplate template)
        {
            // This is a work around for a Beta1 Bug
            // Where under load the LoadContent can fail.
            // This stops us seeing this issue as frequently.
            // The idea is to try upto 5 times to load a template.
            // If it still does not load then rethrow the exception.
            int retryCount = 5;
            DependencyObject dependencyObject = null;
            if (template != null)
            {
                while (null == dependencyObject && 0 != retryCount)
                {
                    try
                    {
                        dependencyObject = template.LoadContent();
                    }
                    catch (System.Windows.Markup.XamlParseException e)
                    {
                        System.Diagnostics.Debug.WriteLine(e.ToString());
                        System.Diagnostics.Debug.WriteLine("\n Template Load Error");
                        retryCount--;
                        System.Threading.Thread.SpinWait(2000);
                        if (retryCount == 0)
                        {
                            throw;
                        }
                    }
                }
            }

            return dependencyObject;
        }
        internal static FrameworkContentElement LoadDataTemplate(DataTemplate dataTemplate)
        {
            object content = dataTemplate.LoadContent();

            if (content is Fragment)
            {
                return (FrameworkContentElement)((Fragment)content).Content;
            }
            else if (content is TextBlock)
            {
                InlineCollection inlines = ((TextBlock)content).Inlines;

                if (inlines.Count == 1)
                {
                    return inlines.FirstInline;
                }
                else
                {
                    Paragraph paragraph = new Paragraph();

                    while (inlines.FirstInline != null)
                    {
                        paragraph.Inlines.Add(inlines.FirstInline);
                    }

                    return paragraph;
                }
            }
            else
            {
                throw new Exception("Data template needs to contain a <Fragment> or <TextBlock>");
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DataTemplateElement"/> class.
        /// </summary>
        /// <param name="dataTemplate">The data template.</param>
        /// <param name="boundType">Type of the bound.</param>
        /// <param name="baseName">Name of the base.</param>
        internal DataTemplateElement(DataTemplate dataTemplate, BoundType boundType, string baseName)
            : base(dataTemplate.LoadContent(), boundType)
        {
            _dataTemplate = dataTemplate;

            if(_dataTemplate.DataTemplateKey != null)
                BaseName = baseName + " [DataTemplate " + _dataTemplate.DataTemplateKey + "] ";

            else BaseName = baseName + " [DataTemplate] ";
        }
Exemple #6
0
 public PrintTable(DataTemplate itemsControlTemplate, IEnumerable itemsSource, PrintTemplate columnsHeader, PrintTemplate tableHeader, PrintTemplate tableFooter, Border border, PrintTemplate caption)
 {
     var control = (ItemsControl) itemsControlTemplate.LoadContent();
     this.itemTemplate = control.ItemTemplate;
     this.itemsSource = itemsSource;
     this.itemsControlTemplate = itemsControlTemplate;
     this.tableFooter = tableFooter;
     this.tableHeader = tableHeader;
     this.columsHeader = columnsHeader;
     this.border = border;
     this.caption = caption;
 }
Exemple #7
0
        void InsertItem(object item, int index, bool newItem)
        {
            DependencyObject pageContent = PageTemplate.LoadContent();

            var          pageItem  = (Page)item;
            PanoramaItem container = GetPageContainer(pageItem);

            if (container == null)
            {
                container = new PanoramaItem {
                    DataContext = item, Content = pageContent
                };

                SetPageContainer(pageItem, container);
            }

            Items.Insert(index, container);
        }
        //, double headerHeight, double footerHeight)
        public PrintPage(PrintLayout pageLayout, Size bodySize, DataTemplate headerTemplate, DataTemplate footerTemplate)
        {
            InitializeComponent();
            ChildRenderTransform = pageLayout.RenderTransform;
            BodySize = bodySize; //new Size(pageLayout.ContentSize.Width, pageLayout.ContentSize.Height - footerHeight - headerHeight);
            Height = pageLayout.Size.Height - pageLayout.Margin.Bottom - pageLayout.Margin.Top;
            Width = pageLayout.Size.Width - pageLayout.Margin.Left - pageLayout.Margin.Right;
            Margin = pageLayout.Margin;

            if (headerTemplate != null)
            {
                this.Header.Children.Add((UIElement) headerTemplate.LoadContent());
            }
            if (footerTemplate != null)
            {
                this.Footer.Children.Add((UIElement) footerTemplate.LoadContent());
            }
        }
 private UIElement GetTemplatedCell(C1FlexGrid grid, DataTemplate dt)
 {
     if (_cacheOwner == null)
     {
         _cacheOwner = grid;
     }
     List<UIElement> list = null;
     if (ReferenceEquals(grid, _cacheOwner) && _dtCache.TryGetValue(dt, out list) && list.Count > 0)
     {
         foreach (UIElement current in list)
         {
             if (VisualTreeHelper.GetParent(current) == null)
             {
                 return current;
             }
         }
     }
     UIElement uiElement = dt.LoadContent() as UIElement;
     if (uiElement != null && ReferenceEquals(grid, _cacheOwner))
     {
         if (list == null)
         {
             list = new List<UIElement>();
             _dtCache[dt] = list;
         }
         list.Add(uiElement);
         _dtRawCache[uiElement] = true;
     }
     return uiElement;
 }
 /// <summary>
 /// Creates the template control.
 /// </summary>
 /// <param name="d">The definition.</param>
 /// <param name="template">The data template.</param>
 /// <returns>A content control.</returns>
 protected virtual FrameworkElement CreateTemplateControl(TemplateCellDefinition d, DataTemplate template)
 {
     var content = (FrameworkElement)template.LoadContent();
     var binding = this.CreateBinding(d);
     binding.Mode = BindingMode.OneWay;
     var c = new ContentControl
     {
         HorizontalAlignment = HorizontalAlignment.Stretch,
         VerticalAlignment = VerticalAlignment.Stretch,
         Content = content
     };
     content.SetBinding(FrameworkElement.DataContextProperty, binding);
     this.SetIsEnabledBinding(d, content);
     this.SetBackgroundBinding(d, c);
     return c;
 }
 public static FrameworkElement Create(DataTemplate template, object dataContext)
 {
     var element = (FrameworkElement) template.LoadContent();
     element.DataContext = dataContext;
     return element;
 }
Exemple #12
0
 protected virtual void OnContentTemplateChanged(DataTemplate oldContentTemplate, DataTemplate newContentTemplate)
 {
     if (this.contentTemplateInstance != null)
     {
         this.contentTemplateInstance.LayoutUpdated -= this.ContentControl_LayoutUpdated;
         this.RemoveContentTemplateInstance();
         this.RemoveLogicalChild(this.contentTemplateInstance);
     }
     if (newContentTemplate != null)
     {
         this.contentTemplateInstance = (UIElement)newContentTemplate.LoadContent();
         this.contentTemplateInstance.LayoutUpdated += this.ContentControl_LayoutUpdated;
         this.AddLogicalChild(this.contentTemplateInstance);
         if (this.Content is UIElement)
         {
             this.NativeChangeUIContentParent();
         }
         this.contentTemplateInstance.DataContext = this.Content;
         this.AddContentTemplateInstance();
     }
     else
     {
         if (this.Content is UIElement)
         {
             this.NativeChangeUIContentParent();
         }
         this.contentTemplateInstance = null;
     }
     this.OnNativeContentChanged(null, this.Content);
     this.OnLayoutUpdated();
 }
        /// <summary>
        /// Loads the data template.
        /// </summary>
        /// <param name="dataTemplate">The data template.</param>
        /// <returns>List{FrameworkContentElement}.</returns>
        public static List<FrameworkContentElement> LoadDataTemplate(DataTemplate dataTemplate)
        {
            var elements = new List<FrameworkContentElement>();

            var documentDataTemplate = dataTemplate as DocumentDataTemplate;
            DependencyObject content;
            if (documentDataTemplate != null)
                content = documentDataTemplate.LoadDocumentTemplate();
            else 
                content = dataTemplate.LoadContent();

            var sp = content as StackPanel;
            if (sp != null)
                foreach (var child in sp.Children)
                {
                    var childDependencyObject = child as DependencyObject;
                    if (childDependencyObject != null)
                        elements.AddRange(ProcessDataTemplateItems(childDependencyObject));
                }
            else 
                elements.AddRange(ProcessDataTemplateItems(content));
            return elements;
        }
Exemple #14
0
        private ListViewItem CreateItem(object dataItem, int index, DataTemplate itemTemplate, Style itemContainerStyle)
        {
            FrameworkElement uiItem = itemTemplate.LoadContent() as FrameworkElement;
            if (uiItem != null) {
                ListViewItem item = new ListViewItem();
                if (itemContainerStyle != null) {
                    item.Style = itemContainerStyle;
                }
                item.DataContext = dataItem;
                item.Content = uiItem;

                _items.Insert(index, item);
                _itemMap[dataItem] = item;

                return item;
            }

            return null;
        }