ContainerFromItem() public method

public ContainerFromItem ( object item ) : DependencyObject
item object
return DependencyObject
Beispiel #1
0
        /// <summary>
        /// Selects the tree view item.
        /// </summary>
        /// <param name="owningItemGenerator">The owning item generator.</param>
        /// <param name="dataItem">The data item.</param>
        public static void SelectTreeViewItem(ItemContainerGenerator owningItemGenerator, object dataItem)
        {
            if (owningItemGenerator == null) return;

            var treeViewItem = owningItemGenerator.ContainerFromItem(dataItem) as TreeViewItem;
            if (treeViewItem != null)
            {
                treeViewItem.IsSelected = true;
            }
        }
Beispiel #2
0
        public UIElement ContainerFromItem(object item)
        {
            ItemContainerGenerator itemContainerGenerator = base.ItemContainerGenerator as ItemContainerGenerator;

            if (itemContainerGenerator != null)
            {
                return(itemContainerGenerator.ContainerFromItem(item) as UIElement);
            }
            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// Indicate whether the specified item is currently visible.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="itemsHostRect">Rect for the item host element.</param>
        /// <param name="listBoxItemRect">Rect for the ListBoxItem element.</param>
        /// <returns>True if the item is visible; false otherwise.</returns>
        /// <remarks>Similar to WPF's corresponding ItemsControl method.</remarks>
        private bool IsOnCurrentPage(object item, out Rect itemsHostRect, out Rect listBoxItemRect)
        {
            // Get Rect for item host element
            DependencyObject ItemsHost = VisualTreeHelper.GetChild(this, 0);

            ItemsHost = VisualTreeHelper.GetChild(ItemsHost, 0);
            FrameworkElement itemsHost =
                (null != TemplateScrollViewer) ?
                ((null != TemplateScrollViewer.ElementScrollContentPresenter) ? TemplateScrollViewer.ElementScrollContentPresenter as FrameworkElement : TemplateScrollViewer as FrameworkElement) :
                ItemsHost as FrameworkElement;

            if (itemsHost == null)
            {
                itemsHostRect   = Rect.Empty;
                listBoxItemRect = Rect.Empty;
                return(false);
            }
            itemsHostRect = new Rect(new Point(), new Point(itemsHost.RenderSize.Width, itemsHost.RenderSize.Height));

            ListBoxItem listBoxItem = (ListBoxItem)ItemContainerGenerator.ContainerFromItem(item);

            if (listBoxItem == null)
            {
                listBoxItemRect = Rect.Empty;
                return(false);
            }

            listBoxItemRect = new Rect(new Point(), listBoxItem.RenderSize);

            // Adjust Rect to account for padding
            Control itemsHostControl = itemsHost as Control;

            if (null != itemsHostControl)
            {
                Thickness padding = itemsHostControl.Padding;
                itemsHostRect = new Rect(
                    itemsHostRect.Left + padding.Left,
                    itemsHostRect.Top + padding.Top,
                    itemsHostRect.Width - padding.Left - padding.Right,
                    itemsHostRect.Height - padding.Top - padding.Bottom);
            }
            // Get relative Rect for ListBoxItem
            GeneralTransform generalTransform = listBoxItem.TransformToVisual(itemsHost);

            if (generalTransform != null)
            {
                listBoxItemRect = new Rect(generalTransform.Transform(new Point()), generalTransform.Transform(new Point(listBoxItem.RenderSize.Width, listBoxItem.RenderSize.Height)));
            }

            // Return result
            return(IsVerticalOrientation() ?
                   (itemsHostRect.Top <= listBoxItemRect.Top) && (listBoxItemRect.Bottom <= itemsHostRect.Bottom) :
                   (itemsHostRect.Left <= listBoxItemRect.Left) && (listBoxItemRect.Right <= itemsHostRect.Right));
        }
Beispiel #4
0
        /// <summary>
        /// Expands the tree view item.
        /// </summary>
        /// <param name="owningItemGenerator">The owning item generator.</param>
        /// <param name="dataItem">The data item.</param>
        /// <returns></returns>
        public static TreeViewItem ExpandTreeViewItem(ItemContainerGenerator owningItemGenerator, object dataItem)
        {
            if (owningItemGenerator == null) return null;

            var treeViewItem = owningItemGenerator.ContainerFromItem(dataItem) as TreeViewItem;
            if (treeViewItem != null)
            {
                treeViewItem.IsExpanded = true;
            }
            return treeViewItem;
        }
        /// <summary>
        /// This is the method that responds to the KeyDown event.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            TabItem nextTabItem = null;

            // Handle [Ctrl][Shift]Tab, Home and End cases
            // We have special handling here because if focus is inside the TabItem content we cannot
            // cycle through TabItem because the content is not part of the TabItem visual tree

            int direction  = 0;
            int startIndex = -1;

            switch (e.Key)
            {
            case Key.Tab:
                if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                {
                    startIndex = ItemContainerGenerator.IndexFromContainer(ItemContainerGenerator.ContainerFromItem(SelectedItem));
                    if ((e.KeyboardDevice.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
                    {
                        direction = -1;
                    }
                    else
                    {
                        direction = 1;
                    }
                }
                break;

            case Key.Home:
                direction  = 1;
                startIndex = -1;
                break;

            case Key.End:
                direction  = -1;
                startIndex = Items.Count;
                break;
            }

            nextTabItem = FindNextTabItem(startIndex, direction);

            if (nextTabItem != null && nextTabItem != SelectedItem)
            {
                e.Handled = nextTabItem.SetFocus();
            }

            if (!e.Handled)
            {
                base.OnKeyDown(e);
            }
        }
 private static TreeViewItem ContainerFromItem(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection, object item)
 {
     foreach (object curChildItem in itemCollection)
     {
         TreeViewItem parentContainer = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);
         TreeViewItem containerThatMightContainItem = (TreeViewItem)parentContainer.ItemContainerGenerator.ContainerFromItem(item);
         if (containerThatMightContainItem != null)
             return containerThatMightContainItem;
         TreeViewItem recursionResult = ContainerFromItem(parentContainer.ItemContainerGenerator, parentContainer.Items, item);
         if (recursionResult != null)
             return recursionResult;
     }
     return null;
 }
 private static object ItemFromContainer(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection, TreeViewItem container)
 {
     foreach (object curChildItem in itemCollection)
     {
         TreeViewItem parentContainer = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);
         TreeViewItem itemThatMightBelongToContainer = (TreeViewItem)parentContainer.ItemContainerGenerator.ItemFromContainer(container);
         if (itemThatMightBelongToContainer != null)
             return itemThatMightBelongToContainer;
         TreeViewItem recursionResult = ItemFromContainer(parentContainer.ItemContainerGenerator, parentContainer.Items, container) as TreeViewItem;
         if (recursionResult != null)
             return recursionResult;
     }
     return null;
 }
Beispiel #8
0
 private static bool Deselect(ItemContainerGenerator itemContainerGenerator)
 {
     if (itemContainerGenerator.Status != GeneratorStatus.ContainersGenerated) return false;
       foreach (object item in itemContainerGenerator.Items)
       {
     TreeViewItem treeViewItem = (TreeViewItem)itemContainerGenerator.ContainerFromItem(item);
     if (treeViewItem.IsSelected)
     {
       treeViewItem.IsSelected = false;
       return true;
     }
     bool retVal = Deselect(treeViewItem.ItemContainerGenerator);
     if (retVal)
       return true;
       }
       return false;
 }
Beispiel #9
0
        private static IEnumerable <TreeViewItem> GetTreeViewItems(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection)
        {
            foreach (object curChildItem in itemCollection)
            {
                TreeViewItem container = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);

                if (container != null)
                {
                    yield return(container);
                }

                foreach (var treeViewItem in GetTreeViewItems(container.ItemContainerGenerator, container.Items))
                {
                    yield return(treeViewItem);
                }
            }
        }
        private void ExpandNextItemInQueueAndSelectIfLastItem(ItemCollection items, ItemContainerGenerator itemContainerGenerator)
        {
            var sampleName = _queueOfIndividualnodesToExpandAndSelectTheLast.Dequeue();
            var sample = items.FirstOrDefault(s => (
                        ((s is Sample) && ((Sample)s).Name == sampleName)
                        || ((s is Group) && ((Group)s).Name == sampleName))
                        );
            if (sample == null)
                return; 
            var container = (TreeViewItem)itemContainerGenerator.ContainerFromItem(sample);

                if (_queueOfIndividualnodesToExpandAndSelectTheLast.Count == 0)
                {
                    container.IsSelected = true;
                }
                else
                {
                    ExpandNextItemInQueueAndSelectIfLastItem(container.Items, container.ItemContainerGenerator);
                }
        }
Beispiel #11
0
 private static object ItemFromContainer(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection, TreeViewItem container)
 {
     foreach (object curChildItem in itemCollection)
     {
         TreeViewItem parentContainer = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);
         if (parentContainer == null)
         {
             return(null);
         }
         TreeViewItem itemThatMightBelongToContainer = (TreeViewItem)parentContainer.ItemContainerGenerator.ItemFromContainer(container);
         if (itemThatMightBelongToContainer != null)
         {
             return(itemThatMightBelongToContainer);
         }
         TreeViewItem recursionResult = ItemFromContainer(parentContainer.ItemContainerGenerator, parentContainer.Items, container) as TreeViewItem;
         if (recursionResult != null)
         {
             return(recursionResult);
         }
     }
     return(null);
 }
Beispiel #12
0
 private static TreeViewItem ContainerFromItem(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection, object item)
 {
     foreach (object curChildItem in itemCollection)
     {
         TreeViewItem parentContainer = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);
         if (parentContainer == null)
         {
             return(null);
         }
         TreeViewItem containerThatMightContainItem = (TreeViewItem)parentContainer.ItemContainerGenerator.ContainerFromItem(item);
         if (containerThatMightContainItem != null)
         {
             return(containerThatMightContainItem);
         }
         TreeViewItem recursionResult = ContainerFromItem(parentContainer.ItemContainerGenerator, parentContainer.Items, item);
         if (recursionResult != null)
         {
             return(recursionResult);
         }
     }
     return(null);
 }
        /// <summary>Finds the TreeViewItem from the collection.</summary>
        /// <param name="parentItemContainerGenerator">The parent item container.</param>
        /// <param name="itemCollection">The item collection.</param>
        /// <param name="predicate">The TreeViewItem.</param>
        /// <returns>The TreeViewItem found.</returns>
        static TreeViewItem FindTreeViewItem(
            ItemContainerGenerator parentItemContainerGenerator, 
            ItemCollection itemCollection, 
            Predicate<TreeViewItem> predicate)
        {
            foreach (var trvItem in
                from object item in itemCollection
                select item as TreeViewItem ?? (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(item))
            {
                if (predicate(trvItem))
                {
                    return trvItem;
                }

                TreeViewItem nestedSearchResult = FindTreeViewItem(
                    trvItem.ItemContainerGenerator, trvItem.Items, predicate);
                if (nestedSearchResult != null)
                {
                    return nestedSearchResult;
                }
            }

            return null;
        }
Beispiel #14
0
        private static TreeViewItem FindContainer(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection, Predicate <TreeViewItem> condition)
        {
            foreach (object curChildItem in itemCollection)
            {
                TreeViewItem containerThatMightMeetTheCondition = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);

                if (containerThatMightMeetTheCondition == null)
                {
                    return(null);
                }

                if (condition(containerThatMightMeetTheCondition))
                {
                    return(containerThatMightMeetTheCondition);
                }

                TreeViewItem recursionResult = FindContainer(containerThatMightMeetTheCondition.ItemContainerGenerator, containerThatMightMeetTheCondition.Items, condition);
                if (recursionResult != null)
                {
                    return(recursionResult);
                }
            }
            return(null);
        }
        /// <summary>
        /// Gets the generated (UI) EnhancedTreeViewItem.
        /// </summary>
        /// <param name="containerGenerator">The ItemContainerGenerator from the "parent".</param>
        /// <param name="item">The (bound) item to get the generated UI Item.</param>
        /// <returns>The generated UI EnhancedTreeViewItem. null if nothing to return.</returns>
        private static EnhancedTreeViewItem ContainerFromItem(ItemContainerGenerator containerGenerator, object item)
        {
            if (containerGenerator != null)
            {
                var container = containerGenerator.ContainerFromItem(item) as EnhancedTreeViewItem;
                if (container != null)
                {
                    return container;
                }
                // Get items from Container through Reflection. This Property is only public in verision > .NET 4.5
                IEnumerable items = containerGenerator.GetPropertyValue("Items") as IEnumerable;
                if (items != null)
                {
                    foreach (var childItem in items)
                    {
                        var parent = containerGenerator.ContainerFromItem(childItem) as EnhancedTreeViewItem;
                        if (parent == null)
                        {
                            continue;
                        }

                        container = parent.ItemContainerGenerator.ContainerFromItem(item) as EnhancedTreeViewItem;
                        if (container != null)
                        {
                            return container;
                        }

                        container = ContainerFromItem(parent.ItemContainerGenerator, item);
                        if (container != null)
                        {
                            return container;
                        }
                    }
                }
            }
            return null;
        }
        private static TreeViewItem ContainerFromItem(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection, object item)
        {
            //todo:needed?
                //if (parentItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.NotStarted)
                //{
                //    IItemContainerGenerator generator = parentItemContainerGenerator;
                //    GeneratorPosition position = generator.GeneratorPositionFromIndex(0);
                //    using (generator.StartAt(position, GeneratorDirection.Forward, true))
                //    {
                //        foreach (object o in itemCollection)
                //        {
                //            DependencyObject dp = generator.GenerateNext();
                //            generator.PrepareItemContainer(dp);
                //        }
                //    }
                //}
                foreach (object curChildItem in itemCollection)
                {
                    TreeViewItem parentContainer = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);
                    if (parentContainer != null)
                    {
                        TreeViewItem containerThatMightContainItem = (TreeViewItem)parentContainer.ItemContainerGenerator.ContainerFromItem(item);

                        if (containerThatMightContainItem != null)
                            return containerThatMightContainItem;
                        TreeViewItem recursionResult = ContainerFromItem(parentContainer.ItemContainerGenerator, parentContainer.Items, item);
                        if (recursionResult != null)
                            return recursionResult;
                    }
                }
                return null;
        }
Beispiel #17
0
        private static TreeViewItem FindContainer(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection, Predicate<TreeViewItem> condition)
        {
            foreach (object curChildItem in itemCollection)
            {
                TreeViewItem containerThatMightMeetTheCondition = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);

                if (containerThatMightMeetTheCondition == null)
                    return null;

                if (condition(containerThatMightMeetTheCondition))
                    return containerThatMightMeetTheCondition;

                TreeViewItem recursionResult = FindContainer(containerThatMightMeetTheCondition.ItemContainerGenerator, containerThatMightMeetTheCondition.Items, condition);
                if (recursionResult != null)
                    return recursionResult;
            }
            return null;
        }
Beispiel #18
0
        private static IEnumerable<TreeViewItem> GetTreeViewItems(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection)
        {
            foreach (object curChildItem in itemCollection)
            {
                TreeViewItem container = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);

                if (container != null)
                    yield return container;

                foreach (var treeViewItem in GetTreeViewItems(container.ItemContainerGenerator, container.Items))
                    yield return treeViewItem;
            }
        }
Beispiel #19
0
 private static TreeViewItem FindTreeViewItem(ItemContainerGenerator itemContainerGenerator, object dataContext)
 {
     if (itemContainerGenerator.Status != GeneratorStatus.ContainersGenerated) return null;
       if (itemContainerGenerator.Items.Contains(dataContext))
     return (TreeViewItem)itemContainerGenerator.ContainerFromItem(dataContext);
       foreach (object item in itemContainerGenerator.Items)
       {
     TreeViewItem treeViewItem = (TreeViewItem)itemContainerGenerator.ContainerFromItem(item);
     TreeViewItem retVal = FindTreeViewItem(treeViewItem.ItemContainerGenerator, dataContext);
     if (retVal != null)
       return retVal;
       }
       return null;
 }
        private TreeViewItem GetTreeViewItem(ItemContainerGenerator containerGenerator, IDataSourceViewModel item)
        {
            var container = (TreeViewItem) containerGenerator.ContainerFromItem(item);
            if (container != null)
                return container;

            foreach (object childItem in containerGenerator.Items)
            {
                var parent = containerGenerator.ContainerFromItem(childItem) as TreeViewItem;
                if (parent == null)
                    continue;

                container = parent.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
                if (container != null)
                    return container;

                container = GetTreeViewItem(parent.ItemContainerGenerator, item);
                if (container != null)
                    return container;
            }
            return null;
        }
Beispiel #21
0
            // update container and index with current values
            internal ItemInfo Refresh(ItemContainerGenerator generator)
            {
                if (Container == null && Index < 0)
                {
                    Container = generator.ContainerFromItem(Item);
                }

                if (Index < 0 && Container != null)
                {
                    Index = generator.IndexFromContainer(Container);
                }

                if (Container == null && Index >= 0)
                {
                    Container = generator.ContainerFromIndex(Index);
                }

                if (Container == SentinelContainer && Index >= 0)
                {
                    Container = null;   // caller explicitly wants null container
                }

                return this;
            }
 private static IEnumerable<TreeViewItem> GetTreeViewItems(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection)
 {
     foreach (object iteratorVariable0 in itemCollection)
     {
         TreeViewItem iteratorVariable1 = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(iteratorVariable0);
         if (iteratorVariable1 != null)
         {
             yield return iteratorVariable1;
             foreach (TreeViewItem iteratorVariable2 in GetTreeViewItems(iteratorVariable1.ItemContainerGenerator, iteratorVariable1.Items))
             {
                 yield return iteratorVariable2;
             }
         }
     }
 }