protected override UIElement GetElementCore(ElementFactoryGetArgs args)
        {
            object newContent;

            {
                object init()
                {
                    if (m_itemTemplateWrapper != null)
                    {
                        return(m_itemTemplateWrapper.GetElement(args));
                    }
                    return(args.Data);
                }

                newContent = init();
            }

            if (newContent is NavigationViewItemBase newItem)
            {
                return(newItem);
            }

            // Create a wrapping container for the data
            var nvi = new NavigationViewItem();

            nvi.Content = newContent;
            return(nvi);
        }
Exemple #2
0
        public UIElement GetElement(ElementFactoryGetArgs args)
        {
            var       selectedTemplate = Template ?? TemplateSelector.SelectTemplate(args.Data, null);
            var       recyclePool      = RecyclePool.GetPoolInstance(selectedTemplate);
            UIElement element          = null;

            if (recyclePool != null)
            {
                // try to get an element from the recycle pool.
                element = recyclePool.TryGetElement(string.Empty /* key */, args.Parent as FrameworkElement);
            }

            if (element == null)
            {
                // no element was found in recycle pool, create a new element
                element = selectedTemplate.LoadContent() as FrameworkElement;

                // Template returned null, so insert empty element to render nothing
                if (element == null)
                {
                    element = new Rectangle {
                        Width  = 0,
                        Height = 0
                    };
                }

                // Associate template with element
                element.SetValue(RecyclePool.OriginTemplateProperty, selectedTemplate);
            }

            return(element);
        }
Exemple #3
0
        protected override UIElement GetElementCore(ElementFactoryGetArgs args)
        {
            object newContent;

            if (m_itemTemplateWrapper != null)
            {
                newContent = m_itemTemplateWrapper.GetElement(args);
            }
            else
            {
                newContent = args.Data;
            }

            // Element is already a RadioButton, so we just return it.
            if (newContent is RadioButton radioButton)
            {
                return(radioButton);
            }

            // Element is not a RadioButton. We'll wrap it in a RadioButton now.
            var newRadioButton = new RadioButton();

            newRadioButton.Content = args.Data;

            // If a user provided item template exists, we pass the template down to the ContentPresenter of the RadioButton.
            if (m_itemTemplateWrapper is ItemTemplateWrapper itemTemplateWrapper)
            {
                newRadioButton.ContentTemplate = itemTemplateWrapper.Template;
            }

            return(newRadioButton);
        }
 protected override UIElement GetElementCore(ElementFactoryGetArgs args)
 {
     if (args.Data is RadioButton radioButton)
     {
         return(radioButton);
     }
     else
     {
         var newRadioButton = new RadioButton();
         newRadioButton.Content = args.Data;
         return(newRadioButton);
     }
 }
Exemple #5
0
        public UIElement GetElement(ElementFactoryGetArgs args)
        {
            var selectedTemplate = Template ?? TemplateSelector.SelectTemplate(args.Data, null);

            // Check if selected template we got is valid
            if (selectedTemplate == null)
            {
                selectedTemplate = TemplateSelector.SelectTemplate(args.Data, null);

                if (selectedTemplate == null)
                {
                    // Still nullptr, fail with a reasonable message now.
                    throw new InvalidOperationException("Null encountered as data template. That is not a valid value for a data template, and can not be used.");
                }
            }
            var       recyclePool = RecyclePool.GetPoolInstance(selectedTemplate);
            UIElement element     = null;

            if (recyclePool != null)
            {
                // try to get an element from the recycle pool.
                element = recyclePool.TryGetElement(string.Empty /* key */, args.Parent as FrameworkElement);
            }

            if (element == null)
            {
                // no element was found in recycle pool, create a new element
                element = selectedTemplate.LoadContent() as FrameworkElement;

                // Template returned null, so insert empty element to render nothing
                if (element == null)
                {
                    element = new Rectangle {
                        Width  = 0,
                        Height = 0
                    };
                }

                // Associate template with element
                element.SetValue(RecyclePool.OriginTemplateProperty, selectedTemplate);
            }

            return(element);
        }
 protected virtual UIElement GetElementCore(ElementFactoryGetArgs args)
 {
     throw new NotImplementedException();
 }
 public UIElement GetElement(ElementFactoryGetArgs args)
 {
     return(GetElementCore(args));
 }
Exemple #8
0
        // Retrieve the element that will be displayed for a specific data item.
        // If the resolved element is not derived from NavigationViewItemBase, wrap in a NavigationViewItem before returning.
        protected override UIElement GetElementCore(ElementFactoryGetArgs args)
        {
            object newContent;

            {
                object init()
                {
                    if (m_itemTemplateWrapper != null)
                    {
                        return(m_itemTemplateWrapper.GetElement(args));
                    }
                    return(args.Data);
                }

                newContent = init();
            }

            // Element is already of expected type, just return it
            if (newContent is NavigationViewItemBase newItem)
            {
                return(newItem);
            }

            // Get or create a wrapping container for the data
            NavigationViewItem nvi;
            {
                nvi = init();
                NavigationViewItem init()
                {
                    if (navigationViewItemPool.Count > 0)
                    {
                        var nvi = navigationViewItemPool.Last();
                        navigationViewItemPool.RemoveLast();
                        return(nvi);
                    }
                    return(new NavigationViewItem());
                }
            }
            var nviImpl = nvi;

            nviImpl.CreatedByNavigationViewItemsFactory = true;

            // If a user provided item template exists, just pass the template and data down to the ContentPresenter of the NavigationViewItem
            if (m_itemTemplateWrapper != null)
            {
                if (m_itemTemplateWrapper is ItemTemplateWrapper itemTemplateWrapper)
                {
                    // Recycle newContent
                    var tempArgs = new ElementFactoryRecycleArgs();
                    tempArgs.Element = newContent as UIElement;
                    m_itemTemplateWrapper.RecycleElement(tempArgs);


                    nviImpl.Content                 = args.Data;
                    nviImpl.ContentTemplate         = itemTemplateWrapper.Template;
                    nviImpl.ContentTemplateSelector = itemTemplateWrapper.TemplateSelector;
                    return(nviImpl);
                }
            }

            nviImpl.Content = newContent;
            return(nviImpl);
        }
Exemple #9
0
        private UIElement GetElementFromElementFactory(int index)
        {
            // The view generator is the provider of last resort.
            var data = m_owner.ItemsSourceView.GetAt(index);

            UIElement initElement()
            {
                var providedElementFactory = m_owner.ItemTemplateShim;

                if (providedElementFactory == null)
                {
                    if (data is UIElement dataAsElement)
                    {
                        return(dataAsElement);
                    }
                }

                IElementFactoryShim initElementFactory()
                {
                    if (providedElementFactory == null)
                    {
                        var factory = XamlReader.Parse("<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><TextBlock Text='{Binding}'/></DataTemplate>") as DataTemplate;
                        m_owner.ItemTemplate = factory;
                        return(m_owner.ItemTemplateShim);
                    }
                    return(providedElementFactory);
                }

                var elementFactory = initElementFactory();

                ElementFactoryGetArgs initArgs()
                {
                    if (m_ElementFactoryGetArgs == null)
                    {
                        m_ElementFactoryGetArgs = new ElementFactoryGetArgs();
                    }
                    return(m_ElementFactoryGetArgs);
                }

                var args = initArgs();

                try
                {
                    args.Data   = data;
                    args.Parent = m_owner;
                    args.Index  = index;

                    return(elementFactory.GetElement(args));
                }
                finally
                {
                    args.Data   = null;
                    args.Parent = null;
                }
            }

            var element = initElement();

            var virtInfo = ItemsRepeater.TryGetVirtualizationInfo(element);

            if (virtInfo == null)
            {
                virtInfo = ItemsRepeater.CreateAndInitializeVirtualizationInfo(element);
            }
            else
            {
                // View obtained from ElementFactory already has a VirtualizationInfo attached to it
                // which means that the element has been recycled and not created from scratch.
            }

            if (data != element)
            {
                if (element is FrameworkElement elementAsFE)
                {
                    // Set data context only if no x:Bind was used. ie. No data template component on the root.
                    // If the passed in data is a UIElement and is different from the element returned by
                    // the template factory then we need to propagate the DataContext.
                    // Otherwise just set the DataContext on the element as the data.
                    object initElementDataContext()
                    {
                        if (data is FrameworkElement dataAsElement)
                        {
                            var dataDataContext = dataAsElement.DataContext;
                            if (dataDataContext != null)
                            {
                                return(dataDataContext);
                            }
                        }
                        return(data);
                    }

                    var elementDataContext = initElementDataContext();

                    elementAsFE.DataContext = elementDataContext;
                }
                else
                {
                    Debug.Assert(false, "Element returned by factory is not a FrameworkElement!");
                }
            }

            virtInfo.MoveOwnershipToLayoutFromElementFactory(
                index,
                /* uniqueId: */
                m_owner.ItemsSourceView.HasKeyIndexMapping ?
                m_owner.ItemsSourceView.KeyFromIndex(index) :
                string.Empty);

            // The view generator is the only provider that prepares the element.
            var repeater = m_owner;

            // Add the element to the children collection here before raising OnElementPrepared so
            // that handlers can walk up the tree in case they want to find their IndexPath in the
            // nested case.
            var children = repeater.Children;

            if (CachedVisualTreeHelpers.GetParent(element) != repeater)
            {
                children.Add(element);
            }

            repeater.AnimationManager.OnElementPrepared(element);

            repeater.OnElementPrepared(element, index);

            // Update realized indices
            m_firstRealizedElementIndexHeldByLayout = Math.Min(m_firstRealizedElementIndexHeldByLayout, index);
            m_lastRealizedElementIndexHeldByLayout  = Math.Max(m_lastRealizedElementIndexHeldByLayout, index);

            return(element);
        }
Exemple #10
0
        private UIElement GetElementFromElementFactory(int index)
        {
            // The view generator is the provider of last resort.
            var data = m_owner.ItemsSourceView.GetAt(index);

            var itemTemplateFactory = m_owner.ItemTemplateShim;

            UIElement element = null;
            bool      itemsSourceContainsElements = false;

            if (itemTemplateFactory == null)
            {
                element = data as UIElement;
                // No item template provided and ItemsSource contains objects derived from UIElement.
                // In this case, just use the data directly as elements.
                itemsSourceContainsElements = element != null;
            }

            if (element == null)
            {
                if (itemTemplateFactory == null)
                {
                    // If no ItemTemplate was provided, use a default
                    var factory = XamlReader.Parse("<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><TextBlock Text='{Binding}'/></DataTemplate>") as DataTemplate;
                    m_owner.ItemTemplate = factory;
                    itemTemplateFactory  = m_owner.ItemTemplateShim;
                }

                if (m_ElementFactoryGetArgs == null)
                {
                    // Create one.
                    m_ElementFactoryGetArgs = new ElementFactoryGetArgs();
                }

                var args = m_ElementFactoryGetArgs;
                args.Data   = data;
                args.Parent = m_owner;
                args.Index  = index;

                element = itemTemplateFactory.GetElement(args);

                args.Data   = null;
                args.Parent = null;
            }

            var virtInfo = ItemsRepeater.TryGetVirtualizationInfo(element);

            if (virtInfo == null)
            {
                virtInfo = ItemsRepeater.CreateAndInitializeVirtualizationInfo(element);
            }
            else
            {
                // View obtained from ElementFactory already has a VirtualizationInfo attached to it
                // which means that the element has been recycled and not created from scratch.
            }

            if (!itemsSourceContainsElements)
            {
                // Set data context only if no x:Bind was used. ie. No data template component on the root.
                var elementAsFE = element as FrameworkElement;
                elementAsFE.DataContext = data;
            }

            virtInfo.MoveOwnershipToLayoutFromElementFactory(
                index,
                /* uniqueId: */
                m_owner.ItemsSourceView.HasKeyIndexMapping ?
                m_owner.ItemsSourceView.KeyFromIndex(index) :
                string.Empty);

            // The view generator is the only provider that prepares the element.
            var repeater = m_owner;

            // Add the element to the children collection here before raising OnElementPrepared so
            // that handlers can walk up the tree in case they want to find their IndexPath in the
            // nested case.
            var children = repeater.Children;

            if (CachedVisualTreeHelpers.GetParent(element) != repeater)
            {
                children.Add(element);
            }

            repeater.AnimationManager.OnElementPrepared(element);

            repeater.OnElementPrepared(element, index);

            // Update realized indices
            m_firstRealizedElementIndexHeldByLayout = Math.Min(m_firstRealizedElementIndexHeldByLayout, index);
            m_lastRealizedElementIndexHeldByLayout  = Math.Max(m_lastRealizedElementIndexHeldByLayout, index);

            return(element);
        }
Exemple #11
0
 protected abstract UIElement GetElementCore(ElementFactoryGetArgs args);