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); }
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; } }
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); } }
/// <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], } } }; }
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); }
/// <inheritdoc/> protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e) { base.OnAttachedToLogicalTree(e); _treeView = this.GetLogicalAncestors().OfType<TreeView>().FirstOrDefault(); }
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)); }
private void InitializeComponent() { PerspexXamlLoader.Load(this); _tree = this.FindControl<TreeView>("tree"); }
/// <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; } }
/// <inheritdoc/> protected override void OnAttachedToVisualTree(IRenderRoot root) { base.OnAttachedToVisualTree(root); _treeView = this.GetVisualAncestors().OfType<TreeView>().FirstOrDefault(); }