Exemplo n.º 1
0
        public void Setting_Items_To_Null_Should_Remove_Containers()
        {
            var target = new ItemsPresenter
            {
                Items = new[] { "foo", "bar" },
            };

            target.ApplyTemplate();
            target.Items = null;

            Assert.Empty(target.Panel.Children);
            Assert.Empty(target.ItemContainerGenerator.Containers);
        }
Exemplo n.º 2
0
        public void Panel_Should_Be_Created_From_ItemsPanel_Template()
        {
            var panel  = new Panel();
            var target = new ItemsPresenter
            {
                ItemsPanel = new FuncTemplate <IPanel>(() => panel),
            };

            target.ApplyTemplate();

            Assert.Same(panel, target.Panel);
            Assert.Same(target, target.Panel.Parent);
        }
Exemplo n.º 3
0
        public void Should_Return_IsLogicalScrollEnabled_False_When_Doesnt_Have_ScrollPresenter_Parent()
        {
            var target = new ItemsPresenter
            {
                ItemsPanel         = VirtualizingPanelTemplate(),
                ItemTemplate       = ItemTemplate(),
                VirtualizationMode = ItemVirtualizationMode.Simple,
            };

            target.ApplyTemplate();

            Assert.False(((ILogicalScrollable)target).IsLogicalScrollEnabled);
        }
Exemplo n.º 4
0
        public void Should_Remove_Containers()
        {
            var items  = new PerspexList <string>(new[] { "foo", "bar" });
            var target = new ItemsPresenter
            {
                Items = items,
            };

            target.ApplyTemplate();
            items.RemoveAt(0);

            Assert.Equal(1, target.Panel.Children.Count);
            Assert.Equal("bar", ((TextBlock)target.Panel.Children[0]).Text);
        }
Exemplo n.º 5
0
        public void Should_Add_Containers_Of_Correct_Type()
        {
            var target = new ItemsPresenter
            {
                Items = new[] { "foo", "bar" },
            };

            target.ItemContainerGenerator = new ItemContainerGenerator <ListBoxItem>(target);
            target.ApplyTemplate();

            Assert.Equal(2, target.Panel.Children.Count);
            Assert.IsType <ListBoxItem>(target.Panel.Children[0]);
            Assert.IsType <ListBoxItem>(target.Panel.Children[1]);
        }
Exemplo n.º 6
0
        public void Should_Add_Containers()
        {
            var target = new ItemsPresenter
            {
                Items = new[] { "foo", "bar" },
            };

            target.ApplyTemplate();

            Assert.Equal(2, target.Panel.Children.Count);
            Assert.IsType <ContentPresenter>(target.Panel.Children[0]);
            Assert.IsType <ContentPresenter>(target.Panel.Children[1]);
            Assert.Equal("foo", ((ContentPresenter)target.Panel.Children[0]).Content);
            Assert.Equal("bar", ((ContentPresenter)target.Panel.Children[1]).Content);
        }
Exemplo n.º 7
0
        public void Should_Remove_Containers()
        {
            var items  = new AvaloniaList <string>(new[] { "foo", "bar" });
            var target = new ItemsPresenter
            {
                Items = items,
            };

            target.ApplyTemplate();
            items.RemoveAt(0);

            Assert.Equal(1, target.Panel.Children.Count);
            Assert.Equal("bar", ((ContentPresenter)target.Panel.Children[0]).Content);
            Assert.Equal("bar", ((ContentPresenter)target.ItemContainerGenerator.ContainerFromIndex(0)).Content);
        }
Exemplo n.º 8
0
        public void Should_Handle_Duplicate_Items()
        {
            var items = new AvaloniaList <int>(new[] { 1, 2, 1 });

            var target = new ItemsPresenter
            {
                Items = items,
            };

            target.ApplyTemplate();
            items.RemoveAt(2);

            var text = target.Panel.Children.OfType <TextBlock>().Select(x => x.Text);

            Assert.Equal(new[] { "1", "2" }, text);
        }
Exemplo n.º 9
0
        public void Clearing_Items_Should_Remove_Containers()
        {
            var items = new ObservableCollection <string> {
                "foo", "bar"
            };
            var target = new ItemsPresenter
            {
                Items = items,
            };

            target.ApplyTemplate();
            items.Clear();

            Assert.Empty(target.Panel.Children);
            Assert.Empty(target.ItemContainerGenerator.Containers);
        }
Exemplo n.º 10
0
        public void MemberSelector_Should_Select_Member()
        {
            var target = new ItemsPresenter
            {
                Items          = new[] { new Item("Foo"), new Item("Bar") },
                MemberSelector = new FuncMemberSelector <Item, string>(x => x.Value),
            };

            target.ApplyTemplate();

            var text = target.Panel.Children
                       .Cast <ContentPresenter>()
                       .Select(x => x.Content)
                       .ToList();

            Assert.Equal(new[] { "Foo", "Bar" }, text);
        }
        public void Should_Create_Containers_Only_Once()
        {
            var parent = new TestItemsControl();
            var target = new ItemsPresenter
            {
                Items = new[] { "foo", "bar" },
                [StyledElement.TemplatedParentProperty] = parent,
            };
            var raised = 0;

            parent.ItemContainerGenerator.Materialized += (s, e) => ++ raised;

            target.ApplyTemplate();

            Assert.Equal(2, target.Panel.Children.Count);
            Assert.Equal(2, raised);
        }
Exemplo n.º 12
0
        public void MemberSelector_Should_Set_DataContext()
        {
            var items  = new[] { new Item("Foo"), new Item("Bar") };
            var target = new ItemsPresenter
            {
                Items          = items,
                MemberSelector = new FuncMemberSelector <Item, string>(x => x.Value),
            };

            target.ApplyTemplate();

            var dataContexts = target.Panel.Children
                               .Cast <TextBlock>()
                               .Select(x => x.DataContext)
                               .ToList();

            Assert.Equal(new[] { "Foo", "Bar" }, dataContexts);
        }
Exemplo n.º 13
0
        public void Should_Handle_Duplicate_Items()
        {
            var items = new AvaloniaList <int>(new[] { 1, 2, 1 });

            var target = new ItemsPresenter
            {
                Items = items,
            };

            target.ApplyTemplate();
            items.RemoveAt(2);

            var numbers = target.Panel.Children
                          .OfType <ContentPresenter>()
                          .Select(x => x.Content)
                          .Cast <int>();

            Assert.Equal(new[] { 1, 2 }, numbers);
        }
Exemplo n.º 14
0
        public void Setting_Presenter_Explicitly_Should_Set_Item_Parent()
        {
            var target = new TestItemsControl();
            var child  = new Control();

            var presenter = new ItemsPresenter
            {
                [StyledElement.TemplatedParentProperty] = target,
                [~ItemsPresenter.ItemsProperty]         = target[~ItemsControl.ItemsProperty],
            };

            presenter.ApplyTemplate();
            target.Presenter = presenter;
            target.Items     = new[] { child };
            target.ApplyTemplate();

            Assert.Equal(target, child.Parent);
            Assert.Equal(target, ((ILogical)child).LogicalParent);
        }
Exemplo n.º 15
0
        private void Rebuild()
        {
            SceneNodeProperty colorProperty = this.editingProperty;

            if (colorProperty != null && !colorProperty.IsMixedValue)
            {
                this.UpdateCurrentTab();
                if (colorProperty.IsResource)
                {
                    colorProperty.SceneNodeObjectSet.InvalidateLocalResourcesCache();
                    this.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Delegate)(o =>
                    {
                        if (this.ResourceList.IsLoaded)
                        {
                            this.ResourceList.ApplyTemplate();
                            ItemsControl itemsControl = (ItemsControl)this.ResourceList.Template.FindName("ResourcesControl", (FrameworkElement)this.ResourceList);
                            itemsControl.ApplyTemplate();
                            ItemsPresenter itemsPresenter = (ItemsPresenter)itemsControl.Template.FindName("ResourcesItemsPresenter", (FrameworkElement)itemsControl);
                            itemsPresenter.ApplyTemplate();
                            WorkaroundVirtualizingStackPanel virtualizingStackPanel = (WorkaroundVirtualizingStackPanel)itemsControl.ItemsPanel.FindName("VirtualizingStackPanel", (FrameworkElement)itemsPresenter);
                            int index = -1;
                            if (colorProperty.SelectedLocalResourceModel != null)
                            {
                                colorProperty.SelectedLocalResourceModel.Parent.IsExpanded = true;
                                index = itemsControl.Items.IndexOf((object)colorProperty.SelectedLocalResourceModel);
                            }
                            else if (colorProperty.SelectedSystemResourceModel != null)
                            {
                                colorProperty.SelectedSystemResourceModel.Parent.IsExpanded = true;
                                index = itemsControl.Items.IndexOf((object)colorProperty.SelectedSystemResourceModel);
                            }
                            if (index >= 0)
                            {
                                virtualizingStackPanel.BringIndexIntoViewWorkaround(index);
                            }
                        }
                        return(null);
                    }), null);
                }
            }
            this.OnPropertyChanged("EditingColor");
        }
Exemplo n.º 16
0
        public void Replacing_Items_Should_Update_Containers()
        {
            var items = new ObservableCollection <string> {
                "foo", "bar", "baz"
            };
            var target = new ItemsPresenter
            {
                Items = items,
            };

            target.ApplyTemplate();
            items[1] = "baz";

            var text = target.Panel.Children
                       .OfType <TextBlock>()
                       .Select(x => x.Text)
                       .ToList();

            Assert.Equal(new[] { "foo", "baz", "baz" }, text);
        }
Exemplo n.º 17
0
        public void Inserting_Items_Should_Update_Containers()
        {
            var items = new ObservableCollection <string> {
                "foo", "bar", "baz"
            };
            var target = new ItemsPresenter
            {
                Items = items,
            };

            target.ApplyTemplate();
            items.Insert(2, "insert");

            var text = target.Panel.Children
                       .OfType <ContentPresenter>()
                       .Select(x => x.Content)
                       .ToList();

            Assert.Equal(new[] { "foo", "bar", "insert", "baz" }, text);
        }
Exemplo n.º 18
0
        public void Should_Handle_Null_Items()
        {
            var items = new PerspexList <string>(new[] { "foo", null, "bar" });

            var target = new ItemsPresenter
            {
                Items = items,
            };

            target.ApplyTemplate();

            var text = target.Panel.Children.OfType <TextBlock>().Select(x => x.Text).ToList();

            Assert.Equal(new[] { "foo", "bar" }, text);

            items.RemoveAt(1);

            text = target.Panel.Children.OfType <TextBlock>().Select(x => x.Text).ToList();
            Assert.Equal(new[] { "foo", "bar" }, text);
        }
Exemplo n.º 19
0
        //<Snippet1>
        /// <summary>
        /// Recursively search for an item in this subtree.
        /// </summary>
        /// <param name="container">
        /// The parent ItemsControl. This can be a TreeView or a TreeViewItem.
        /// </param>
        /// <param name="item">
        /// The item to search for.
        /// </param>
        /// <returns>
        /// The TreeViewItem that contains the specified item.
        /// </returns>
        private TreeViewItem GetTreeViewItem(ItemsControl container, object item)
        {
            if (container != null)
            {
                if (container.DataContext == item)
                {
                    return(container as TreeViewItem);
                }

                // Expand the current container
                if (container is TreeViewItem && !((TreeViewItem)container).IsExpanded)
                {
                    container.SetValue(TreeViewItem.IsExpandedProperty, true);
                }

                // Try to generate the ItemsPresenter and the ItemsPanel.
                // by calling ApplyTemplate.  Note that in the
                // virtualizing case even if the item is marked
                // expanded we still need to do this step in order to
                // regenerate the visuals because they may have been virtualized away.

                container.ApplyTemplate();
                ItemsPresenter itemsPresenter =
                    (ItemsPresenter)container.Template.FindName("ItemsHost", container);
                if (itemsPresenter != null)
                {
                    itemsPresenter.ApplyTemplate();
                }
                else
                {
                    // The Tree template has not named the ItemsPresenter,
                    // so walk the descendents and find the child.
                    itemsPresenter = FindVisualChild <ItemsPresenter>(container);
                    if (itemsPresenter == null)
                    {
                        container.UpdateLayout();

                        itemsPresenter = FindVisualChild <ItemsPresenter>(container);
                    }
                }

                Panel itemsHostPanel = (Panel)VisualTreeHelper.GetChild(itemsPresenter, 0);

                // Ensure that the generator for this panel has been created.
                UIElementCollection children = itemsHostPanel.Children;

                MyVirtualizingStackPanel virtualizingPanel =
                    itemsHostPanel as MyVirtualizingStackPanel;

                for (int i = 0, count = container.Items.Count; i < count; i++)
                {
                    TreeViewItem subContainer;
                    if (virtualizingPanel != null)
                    {
                        // Bring the item into view so
                        // that the container will be generated.
                        virtualizingPanel.BringIntoView(i);

                        subContainer =
                            (TreeViewItem)container.ItemContainerGenerator.
                            ContainerFromIndex(i);
                    }
                    else
                    {
                        subContainer =
                            (TreeViewItem)container.ItemContainerGenerator.
                            ContainerFromIndex(i);

                        // Bring the item into view to maintain the
                        // same behavior as with a virtualizing panel.
                        subContainer.BringIntoView();
                    }

                    if (subContainer != null)
                    {
                        // Search the next level for the object.
                        TreeViewItem resultContainer = GetTreeViewItem(subContainer, item);
                        if (resultContainer != null)
                        {
                            return(resultContainer);
                        }
                        else
                        {
                            // The object is not under this TreeViewItem
                            // so collapse it.
                            subContainer.IsExpanded = false;
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 20
0
        private void SelectTreeviewItemByMatch(Func <AsfPacket, int> matchPacketMethod, Func <AsfPacket, int> matchPacketFollowupMethod = null, Func <List <PayloadInfo>, int> matchPayloadMethod = null)
        {
            AsfHierarchy.UpdateLayout();

            foreach (AsfHeaderItem item in AsfHierarchy.Items)
            {
                if (item.Name == "Data Object")
                {
                    AsfDataObjectItem asfDataObjectItem = item as AsfDataObjectItem;

                    TreeViewItem treeViewItem = AsfHierarchy.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
                    if (treeViewItem == null)
                    {
                        return;
                    }
                    treeViewItem.IsExpanded = true;
                    treeViewItem.ApplyTemplate();
                    ItemsPresenter itemsPresenter = (ItemsPresenter)treeViewItem.Template.FindName("ItemsHost", treeViewItem);
                    if (itemsPresenter != null)
                    {
                        itemsPresenter.ApplyTemplate();
                    }
                    else
                    {
                        // The Tree template has not named the ItemsPresenter,
                        // so walk the descendents and find the child.
                        itemsPresenter = FindVisualChild <ItemsPresenter>(treeViewItem);
                        if (itemsPresenter == null)
                        {
                            treeViewItem.UpdateLayout();

                            itemsPresenter = FindVisualChild <ItemsPresenter>(treeViewItem);
                        }
                    }
                    Panel itemsHostPanel = (Panel)VisualTreeHelper.GetChild(itemsPresenter, 0);
                    // Ensure that the generator for this panel has been created.
                    UIElementCollection children = itemsHostPanel.Children;

                    MyVirtualizingStackPanel msp = itemsHostPanel as MyVirtualizingStackPanel;

                    //binary search for right payload for the time offset requested with a 50 millisecond bucket size
                    int index = 0;

                    index = asfDataObjectItem.Packets.BinarySearchForMatch(matchPacketMethod);
                    if (matchPacketFollowupMethod != null)
                    {
                        index = asfDataObjectItem.Packets.LinearSearchForMatch(index, matchPacketFollowupMethod);
                    }

                    if (index >= 0 && index < asfDataObjectItem.Packets.Count)
                    {
                        msp.BringIntoView(index);
                        var  selectedPacket = treeViewItem.ItemContainerGenerator.ContainerFromIndex(index);
                        bool bBuilt         = false;

                        if (selectedPacket != null)
                        {
                            TreeViewItem packetTreeViewItem = selectedPacket as TreeViewItem;
                            //packetTreeViewItem.IsSelected = true;
                            packetTreeViewItem.IsExpanded = true;

                            //now expand to payload
                            bBuilt         = packetTreeViewItem.ApplyTemplate();
                            itemsPresenter = (ItemsPresenter)packetTreeViewItem.Template.FindName("ItemsHost", packetTreeViewItem);
                            if (itemsPresenter != null)
                            {
                                bBuilt = itemsPresenter.ApplyTemplate();
                            }
                            else
                            {
                                // The Tree template has not named the ItemsPresenter,
                                // so walk the descendents and find the child.
                                itemsPresenter = FindVisualChild <ItemsPresenter>(packetTreeViewItem);
                                if (itemsPresenter == null)
                                {
                                    packetTreeViewItem.UpdateLayout();
                                    itemsPresenter = FindVisualChild <ItemsPresenter>(packetTreeViewItem);
                                }
                            }
                            itemsHostPanel = (Panel)VisualTreeHelper.GetChild(itemsPresenter, 0);
                            // Ensure that the generator for this panel has been created.
                            children = itemsHostPanel.Children;
                            msp      = itemsHostPanel as MyVirtualizingStackPanel;

                            int payloadIndex = 0;

                            if (matchPayloadMethod != null)
                            {
                                payloadIndex = matchPayloadMethod(asfDataObjectItem.Packets[index].Payload);
                            }

                            if (payloadIndex < 0 || payloadIndex >= asfDataObjectItem.Packets[index].Payload.Count)
                            {
                                payloadIndex = 0;
                            }

                            msp.BringIntoView(payloadIndex);
                            var          selectedPayload     = packetTreeViewItem.ItemContainerGenerator.ContainerFromIndex(payloadIndex);
                            TreeViewItem payloadTreeViewItem = selectedPayload as TreeViewItem;
                            if (payloadTreeViewItem != null)
                            {
                                payloadTreeViewItem.BringIntoView();
                                payloadTreeViewItem.IsSelected = true;
                                payloadTreeViewItem.IsExpanded = true;
                                payloadTreeViewItem.Focus();
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// Navigates to.
        /// http://stackoverflow.com/questions/183636/selecting-a-node-in-virtualized-treeview-with-wpf
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns><see cref="bool"/></returns>
        public bool NavigateTo(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            FolderService ds = Factory.Resolve <FolderService>();

            FileData root =
                ds.Tree.FirstOrDefault(
                    x => path.StartsWith(x.FullPath + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase));

            if (root == null)
            {
                return(false);
            }

            string[]        pathParts = FolderNavigationService.GetPathParts(path.Substring(root.FullPath.Length));
            List <FileData> tree      = new List <FileData> {
                root
            };

            FileData current = root;

            for (int i = 0; i < pathParts.Length; i++)
            {
                FileData next = current.Children.FirstOrDefault(
                    x => x.IsDirectory == (i != pathParts.Length - 1) &&
                    string.Equals(x.Name, pathParts[i], StringComparison.OrdinalIgnoreCase));
                tree.Add(next);
                current = next;

                if (next == null)
                {
                    return(false);
                }
            }

            ItemsControl currentParent = this.TreeView;

            foreach (FileData node in tree)
            {
                // first try the easy way
                TreeViewItem newParent = currentParent.ItemContainerGenerator.ContainerFromItem(node) as TreeViewItem;
                if (newParent == null)
                {
                    // if this failed, it's probably because of virtualization, and we will have to do it the hard way.
                    // this code is influenced by TreeViewItem.ExpandRecursive decompiled code, and the MSDN sample at http://code.msdn.microsoft.com/Changing-selection-in-a-6a6242c8/sourcecode?fileId=18862&pathId=753647475
                    // see also the question at http://stackoverflow.com/q/183636/46635
                    currentParent.ApplyTemplate();
                    ItemsPresenter itemsPresenter =
                        (ItemsPresenter)currentParent.Template.FindName("ItemsHost", currentParent);
                    if (itemsPresenter != null)
                    {
                        itemsPresenter.ApplyTemplate();
                    }
                    else
                    {
                        currentParent.UpdateLayout();
                    }

                    VirtualizingPanel virtualizingPanel =
                        FolderNavigationService.GetItemsHost(currentParent) as VirtualizingPanel;

                    if (virtualizingPanel == null)
                    {
                        return(false);
                    }

                    FolderNavigationService.CallEnsureGenerator(virtualizingPanel);
                    int index = currentParent.Items.IndexOf(node);
                    if (index < 0)
                    {
                        throw new InvalidOperationException("Node '" + node + "' cannot be found in container");
                    }
                    virtualizingPanel.BringIndexIntoViewPublic(index);
                    newParent = currentParent.ItemContainerGenerator.ContainerFromIndex(index) as TreeViewItem;
                }

                if (newParent == null)
                {
                    throw new InvalidOperationException(
                              "Tree view item cannot be found or created for node '" + node + "'");
                }

                if (node == tree.Last())
                {
                    newParent.IsSelected = true;
                    newParent.BringIntoView();
                    break;
                }

                newParent.IsExpanded = true;
                currentParent        = newParent;
            }
            return(true);
        }
Exemplo n.º 22
0
        // this method is based on and modified from:
        // https://docs.microsoft.com/en-us/dotnet/desktop/wpf/controls/how-to-find-a-treeviewitem-in-a-treeview

        /// <summary>
        /// Recursively search for an item in this subtree.
        /// </summary>
        /// <param name="container">
        /// The parent ItemsControl. This can be a TreeView or a TreeViewItem.
        /// </param>
        /// <param name="item">
        /// The item to search for.
        /// </param>
        /// <returns>
        /// The TreeViewItem that contains the specified item.
        /// </returns>
        private static TreeViewItem GetTreeViewItem(ItemsControl container, object item, object selectedItem, SearchDirection dir, int depth)
        {
            if (container != null)
            {
                if (container.DataContext == item)
                {
                    return(container as TreeViewItem);
                }
                else if (depth <= 0)
                {
                    return(null);
                }

                // Expand the current container
                if (container is TreeViewItem && !((TreeViewItem)container).IsExpanded)
                {
                    container.SetValue(TreeViewItem.IsExpandedProperty, true);
                }

                // Try to generate the ItemsPresenter and the ItemsPanel.
                // by calling ApplyTemplate.  Note that in the
                // virtualizing case even if the item is marked
                // expanded we still need to do this step in order to
                // regenerate the visuals because they may have been virtualized away.

                container.ApplyTemplate();
                ItemsPresenter itemsPresenter =
                    (ItemsPresenter)container.Template.FindName("ItemsHost", container);
                if (itemsPresenter != null)
                {
                    itemsPresenter.ApplyTemplate();
                }
                else
                {
                    // The Tree template has not named the ItemsPresenter,
                    // so walk the descendants and find the child.
                    itemsPresenter = FindVisualChild <ItemsPresenter>(container);
                    if (itemsPresenter == null)
                    {
                        container.UpdateLayout();

                        itemsPresenter = FindVisualChild <ItemsPresenter>(container);
                    }
                }

                Panel itemsHostPanel = (Panel)VisualTreeHelper.GetChild(itemsPresenter, 0);

                // Ensure that the generator for this panel has been created.
                _ = itemsHostPanel.Children;

                int startIndex = -1;
                if (selectedItem != null)
                {
                    startIndex = IndexForItem(container.ItemContainerGenerator, selectedItem);
                }

                int count = container.Items.Count;
                int inc   = dir == SearchDirection.Down ? 1 : -1;
                int idx   = startIndex > -1 ? startIndex : dir == SearchDirection.Down ? 0 : count - 1;
                for (; idx < count && idx >= 0; idx += inc)
                {
                    TreeViewItem subContainer;
                    if (itemsHostPanel is MyVirtualizingStackPanel virtualizingPanel)
                    {
                        // Bring the item into view so
                        // that the container will be generated.
                        virtualizingPanel.BringIntoView(idx);

                        subContainer =
                            (TreeViewItem)container.ItemContainerGenerator.
                            ContainerFromIndex(idx);
                    }
                    else
                    {
                        subContainer =
                            (TreeViewItem)container.ItemContainerGenerator.
                            ContainerFromIndex(idx);
                        if (subContainer != null)
                        {
                            // Bring the item into view to maintain the
                            // same behavior as with a virtualizing panel.
                            subContainer.BringIntoView();
                        }
                    }

                    if (subContainer != null)
                    {
                        // Search the next level for the object.
                        TreeViewItem resultContainer = GetTreeViewItem(subContainer, item, selectedItem, dir, depth - 1);
                        if (resultContainer != null)
                        {
                            return(resultContainer);
                        }
                        else
                        {
                            // The object is not under this TreeViewItem
                            // so collapse it.
                            subContainer.IsExpanded = false;
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 23
0
 private void Rebuild()
 {
     if (this.editingProperty == null)
     {
         this.BrushSubtypeEditor = (BrushSubtypeEditor)null;
     }
     else
     {
         if (!this.editingProperty.Associated)
         {
             return;
         }
         this.oldNichedState  = this.editingProperty.IsMixedValue;
         this.editingResource = false;
         bool flag = this.brushSubtypeEditor is BrushEditor.NullBrushEditor;
         if (this.editingProperty.IsResource)
         {
             this.editingResource = true;
             this.editingProperty.SceneNodeObjectSet.InvalidateLocalResourcesCache();
             this.BrushSubtypeEditor = (BrushSubtypeEditor)null;
             this.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Delegate)(o =>
             {
                 if (this.editingProperty != null && this.ResourceList.IsLoaded && this.Parent != null)
                 {
                     this.ResourceList.ApplyTemplate();
                     ItemsControl itemsControl = (ItemsControl)this.ResourceList.Template.FindName("ResourcesControl", (FrameworkElement)this.ResourceList);
                     itemsControl.ApplyTemplate();
                     ItemsPresenter itemsPresenter = (ItemsPresenter)itemsControl.Template.FindName("ResourcesItemsPresenter", (FrameworkElement)itemsControl);
                     itemsPresenter.ApplyTemplate();
                     WorkaroundVirtualizingStackPanel virtualizingStackPanel = (WorkaroundVirtualizingStackPanel)itemsControl.ItemsPanel.FindName("VirtualizingStackPanel", (FrameworkElement)itemsPresenter);
                     int index = -1;
                     if (this.editingProperty.SelectedLocalResourceModel != null)
                     {
                         this.editingProperty.SelectedLocalResourceModel.Parent.IsExpanded = true;
                         index = itemsControl.Items.IndexOf((object)this.editingProperty.SelectedLocalResourceModel);
                     }
                     else if (this.editingProperty.SelectedSystemResourceModel != null)
                     {
                         this.editingProperty.SelectedSystemResourceModel.Parent.IsExpanded = true;
                         index = itemsControl.Items.IndexOf((object)this.editingProperty.SelectedSystemResourceModel);
                     }
                     if (index >= 0)
                     {
                         virtualizingStackPanel.BringIndexIntoViewWorkaround(index);
                     }
                 }
                 return((object)null);
             }), (object)null);
         }
         else if (this.editingProperty.IsMixedValue)
         {
             this.BrushSubtypeEditor = (BrushSubtypeEditor)null;
         }
         else
         {
             ITypeId typeId = (ITypeId)this.editingProperty.ComputedValueTypeId;
             if (typeId != null)
             {
                 if (typeId.Equals((object)PlatformTypes.SolidColorBrush) && !(this.BrushSubtypeEditor is SolidColorBrushEditor))
                 {
                     this.BrushSubtypeEditor = (BrushSubtypeEditor) new SolidColorBrushEditor(this, this.editingProperty);
                 }
                 else if (PlatformTypes.GradientBrush.IsAssignableFrom(typeId))
                 {
                     if (this.BrushSubtypeEditor == null || !typeId.Equals((object)this.BrushSubtypeEditor.Category.Type))
                     {
                         this.BrushSubtypeEditor = (BrushSubtypeEditor) new GradientBrushEditor(this, typeId, this.editingProperty);
                     }
                 }
                 else if (PlatformTypes.TileBrush.IsAssignableFrom(typeId) && (this.BrushSubtypeEditor == null || !typeId.Equals((object)this.BrushSubtypeEditor.Category.Type)))
                 {
                     this.BrushSubtypeEditor = (BrushSubtypeEditor) new TileBrushEditor(this, typeId, this.editingProperty);
                 }
                 if (this.BrushSubtypeEditor != null)
                 {
                     BrushCategory.SetLastUsed(this.BrushSubtypeEditor.Category, this.editingProperty.GetValue());
                 }
             }
             else
             {
                 this.BrushSubtypeEditor = (BrushSubtypeEditor) new BrushEditor.NullBrushEditor();
             }
         }
         if (!(this.brushSubtypeEditor is BrushEditor.NullBrushEditor) && flag)
         {
             this.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Delegate)(o =>
             {
                 this.BringIntoView();
                 return((object)null);
             }), (object)null);
         }
         this.OnEditingTypeChange();
     }
 }
Exemplo n.º 24
0
        public static TreeViewItem GetTreeViewItem(ItemsControl container, RegistryKeyItemBase item)
        {
            if (container != null)
            {
                if (container.DataContext == item)
                {
                    return(container as TreeViewItem);
                }

                // Expand the current container
                if (container is TreeViewItem && !((TreeViewItem)container).IsExpanded)
                {
                    container.SetValue(TreeViewItem.IsExpandedProperty, true);
                }

                // Try to generate the ItemsPresenter and the ItemsPanel.
                // by calling ApplyTemplate.  Note that in the
                // virtualizing case even if the item is marked
                // expanded we still need to do this step in order to
                // regenerate the visuals because they may have been virtualized away.

                container.ApplyTemplate();
                ItemsPresenter itemsPresenter =
                    (ItemsPresenter)container.Template.FindName("ItemsHost", container);
                if (itemsPresenter != null)
                {
                    itemsPresenter.ApplyTemplate();
                }
                else
                {
                    // The Tree template has not named the ItemsPresenter,
                    // so walk the descendents and find the child.
                    itemsPresenter = FindVisualChild <ItemsPresenter>(container);
                    if (itemsPresenter == null)
                    {
                        container.UpdateLayout();

                        itemsPresenter = FindVisualChild <ItemsPresenter>(container);
                    }
                }

                Panel itemsHostPanel = (Panel)VisualTreeHelper.GetChild(itemsPresenter, 0);


                // Ensure that the generator for this panel has been created.
                UIElementCollection children = itemsHostPanel.Children;

                var virtualizingPanel = itemsHostPanel as VirtualizingStackPanelEx;
                Debug.Assert(virtualizingPanel != null);

                int count = container.Items.Count;
                int index1 = 0, index2 = count;
                while (index1 != index2)
                {
                    int          newindex = (index1 + index2) / 2;
                    TreeViewItem subContainer;
                    if (virtualizingPanel != null)
                    {
                        // Bring the item into view so
                        // that the container will be generated.
                        virtualizingPanel.BringIntoView(newindex);

                        subContainer =
                            (TreeViewItem)container.ItemContainerGenerator.
                            ContainerFromIndex(newindex);
                    }
                    else
                    {
                        subContainer =
                            (TreeViewItem)container.ItemContainerGenerator.
                            ContainerFromIndex(newindex);

                        // Bring the item into view to maintain the
                        // same behavior as with a virtualizing panel.
                        subContainer.BringIntoView();
                    }

                    if (subContainer.DataContext == item)
                    {
                        return(subContainer);
                    }

                    if (item.Text.CompareTo((subContainer.DataContext as RegistryKeyItemBase).Text) > 0)
                    {
                        index1 = newindex;
                    }
                    else
                    {
                        index2 = newindex;
                    }
                }

                //if(subContainer != null && subContainer.IsExpanded) {
                //	// Search the next level for the object.
                //	TreeViewItem resultContainer = GetTreeViewItem(subContainer, item);
                //	if(resultContainer != null) {
                //		return resultContainer;
                //	}
                //	else {
                //		// The object is not under this TreeViewItem
                //		// so collapse it.
                //		//subContainer.IsExpanded = false;
                //	}

                //}
            }

            return(null);
        }
Exemplo n.º 25
0
        /// <summary>
        /// 查找TreeView中TreeViewItem
        /// <para>警告:该方法只能用于TreeView.ItemsHost、TreeView.ItemContainerStyle中ItemsPanel的对象为 CustomVirtualizingStackPanel 方为有效,否则会发生异常</para>
        /// </summary>
        /// <param name="container"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        public static TreeViewItem GetTreeViewItem(this ItemsControl container, object item)
        {
            if (container != null)
            {
                if (container.DataContext == item)
                {
                    return(container as TreeViewItem);
                }
                if (container.Items.Count == 0)
                {
                    return(null);
                }
                if (container is TreeViewItem && !((TreeViewItem)container).IsExpanded)
                {
                    container.SetValue(TreeViewItem.IsExpandedProperty, true);
                }
                container.ApplyTemplate();
                ItemsPresenter itemsPresenter = (ItemsPresenter)container.Template.FindName("ItemsHost", container);
                if (itemsPresenter != null)
                {
                    itemsPresenter.ApplyTemplate();
                }
                else
                {
                    itemsPresenter = FindVisualChild <ItemsPresenter>(container);
                    if (itemsPresenter == null)
                    {
                        container.UpdateLayout();
                        itemsPresenter = FindVisualChild <ItemsPresenter>(container);
                    }
                }

                Panel itemsHostPanel = (Panel)VisualTreeHelper.GetChild(itemsPresenter, 0);
                CustomVirtualizingStackPanel virtualizingPanel = itemsHostPanel as CustomVirtualizingStackPanel;
                for (int i = 0, count = container.Items.Count; i < count; i++)
                {
                    TreeViewItem subContainer;
                    if (virtualizingPanel != null)
                    {
                        virtualizingPanel.BringIntoView(i);
                        subContainer = (TreeViewItem)container.ItemContainerGenerator.ContainerFromIndex(i);
                    }
                    else
                    {
                        subContainer = (TreeViewItem)container.ItemContainerGenerator.ContainerFromIndex(i);
                        subContainer.BringIntoView();
                    }

                    if (subContainer != null)
                    {
                        TreeViewItem resultContainer = GetTreeViewItem(subContainer, item);
                        if (resultContainer != null)
                        {
                            return(resultContainer);
                        }
                        else
                        {
                            subContainer.IsExpanded = false;
                        }
                    }
                }
            }
            return(null);
        }