예제 #1
0
        public void DataContexts_Should_Be_Correctly_Set()
        {
            var items = new object[]
            {
                "Foo",
                new Item("Bar"),
                new TextBlock { Text = "Baz" },
                new TreeViewItem { Header = "Qux" },
            };

            var target = new TreeView
            {
                Template = new ControlTemplate(CreateTreeViewTemplate),
                DataContext = "Base",
                DataTemplates = new DataTemplates
                {
                    new FuncDataTemplate<Item>(x => new Button { Content = x })
                },
                Items = items,
            };

            target.ApplyTemplate();

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

            Assert.Equal(
                new object[] { items[0], items[1], "Base", "Base" },
                dataContexts);
        }
예제 #2
0
        protected override void OnVisualParentChanged(Visual oldParent)
        {
            if (this.GetVisualParent() != null)
            {
                this.treeView = this.GetVisualAncestors().OfType<TreeView>().FirstOrDefault();

                if (this.treeView == null)
                {
                    throw new InvalidOperationException("TreeViewItems must be added to a TreeView.");
                }
            }
            else
            {
                this.treeView = null;
            }
        }
예제 #3
0
        public void LogicalChildren_Should_Be_Set()
        {
            var target = new TreeView
            {
                Template = new ControlTemplate(CreateTreeViewTemplate),
                Items = new[] { "Foo", "Bar", "Baz " },
            };

            target.ApplyTemplate();

            Assert.Equal(3, target.GetLogicalChildren().Count());

            foreach (var child in target.GetLogicalChildren())
            {
                Assert.IsType<TreeViewItem>(child);
            }
        }
예제 #4
0
 /// <summary>
 /// The default template for the <see cref="TreeView"/> control.
 /// </summary>
 /// <param name="control">The control being styled.</param>
 /// <returns>The root of the instantiated template.</returns>
 public static Control Template(TreeView control)
 {
     return new Border
     {
         Padding = new Thickness(4),
         [~Border.BackgroundProperty] = control[~TreeView.BackgroundProperty],
         [~Border.BorderBrushProperty] = control[~TreeView.BorderBrushProperty],
         [~Border.BorderThicknessProperty] = control[~TreeView.BorderThicknessProperty],
         Child = new ScrollViewer
         {
             CanScrollHorizontally = true,
             Content = new ItemsPresenter
             {
                 Name = "itemsPresenter",
                 [~ItemsPresenter.ItemsProperty] = control[~TreeView.ItemsProperty],
                 [~ItemsPresenter.ItemsPanelProperty] = control[~TreeView.ItemsPanelProperty],
             }
         }
     };
 }
예제 #5
0
        private void InitializeComponent()
        {
            TreeView tree;

            Content = new Grid
            {
                ColumnDefinitions = new ColumnDefinitions
                {
                    new ColumnDefinition(1, GridUnitType.Star),
                    new ColumnDefinition(4, GridUnitType.Pixel),
                    new ColumnDefinition(3, GridUnitType.Star),
                },
                Children = new Controls
                {
                    (tree = new TreeView
                    {
                        DataTemplates = new DataTemplates
                        {
                            new FuncTreeDataTemplate<VisualTreeNode>(GetHeader, x => x.Children),
                        },
                        [!ItemsControl.ItemsProperty] = this.WhenAnyValue(x => x.ViewModel.Nodes),
                    }),
                    new GridSplitter
                    {
                        Width = 4,
                        Orientation = Orientation.Vertical,
                        [Grid.ColumnProperty] = 1,
                    },
                    new ContentControl
                    {
                        [!ContentProperty] = this.WhenAnyValue(x => x.ViewModel.Details),
                        [Grid.ColumnProperty] = 2,
                    }
                }
            };

            tree.GetObservable(TreeView.SelectedItemProperty)
                .OfType<VisualTreeNode>()
                .Subscribe(x => ViewModel.SelectedNode = x);
        }
예제 #6
0
 /// <inheritdoc/>
 protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
 {
     base.OnAttachedToLogicalTree(e);
     _treeView = this.GetLogicalAncestors().OfType<TreeView>().FirstOrDefault();
 }
예제 #7
0
        public void TreeView_Is_Freed()
        {
            Func<Window> run = () =>
            {
                var nodes = new[]
                {
                    new Node
                    {
                        Children = new[] { new Node() },
                    }
                };

                TreeView target;

                var window = new Window
                {
                    Content = target = new TreeView
                    {
                        DataTemplates = new DataTemplates
                        {
                            new FuncTreeDataTemplate<Node>(
                                x => new TextBlock { Text = x.Name },
                                x => x.Children,
                                x => true)
                        },
                        Items = nodes
                    }
                };

                // Do a layout and make sure that TreeViewItems get realized.
                window.LayoutManager.ExecuteLayoutPass();
                Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());

                // Clear the content and ensure the TreeView is removed.
                window.Content = null;
                window.LayoutManager.ExecuteLayoutPass();
                Assert.Null(window.Presenter.Child);

                return window;
            };

            var result = run();

            dotMemory.Check(memory =>
                Assert.Equal(0, memory.GetObjects(where => where.Type.Is<TreeView>()).ObjectsCount));
        }
예제 #8
0
 private void InitializeComponent()
 {
     PerspexXamlLoader.Load(this);
     _tree = this.FindControl<TreeView>("tree");
 }
예제 #9
0
        /// <inheritdoc/>
        protected override void OnAttachedToVisualTree(IRenderRoot root)
        {
            base.OnAttachedToVisualTree(root);

            if (this.GetVisualParent() != null)
            {
                this.treeView = this.GetVisualAncestors().OfType<TreeView>().FirstOrDefault();

                if (this.treeView == null)
                {
                    throw new InvalidOperationException("TreeViewItems must be added to a TreeView.");
                }
            }
            else
            {
                this.treeView = null;
            }
        }
예제 #10
0
 /// <inheritdoc/>
 protected override void OnAttachedToVisualTree(IRenderRoot root)
 {
     base.OnAttachedToVisualTree(root);
     _treeView = this.GetVisualAncestors().OfType<TreeView>().FirstOrDefault();
 }