Hi there, I need to implement a custom treeview - The purpose of customization is to let TreeViewNode to host other TreeViewNode's Can anyone suggest on the best way to accomplish that
Thanks,
Dmitry
Windows Presentation Foundation (WPF)
Hi there, I need to implement a custom treeview - The purpose of customization is to let TreeViewNode to host other TreeViewNode's Can anyone suggest on the best way to accomplish that
Thanks,
Dmitry
Do you mean TreeViewItem, or is TreeViewNode your own class
In either case, you should be able to use the built-in TreeView. TreeViewItems can hold onto other TreeViewItems in their Items collection. If you are using data TreeViewNodes, you can create a HierarchicalDataTemplate to create the TreeViewItem containers.
Let us know if you need more help.
Ben
Hi Ben, here is what I'm looking for. I have a customized TreeViewItem and want its children to be added righ into it, using it as a UI container. In the template below I marked the area I want to insert children into with Green. Despite of supplying a valid tree - it doesn't show anything except of the root. Must be some bad misunderstanding of mine...
Thanks.
P.S. Special thanks to J.S., whos saample I used.
TreeView's template:
<
Window x:Class="CustomTreeViewLayout.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:CustomTreeViewLayout" Title="Custom TreeView" Height="350" Width="780" Loaded="OnLoaded" WindowStartupLocation="CenterScreen" FontSize="11">
<
TreeView Name="tree" Background="LightGray"><
TreeView.Resources><
ResourceDictionary><!--1-->
<
ResourceDictionary.MergedDictionaries><
ResourceDictionary Source="Node.xaml" /></
ResourceDictionary.MergedDictionaries><!--
This template explains how to rendera Node object and its child nodes.
--><
HierarchicalDataTemplate DataType="{x:Type local:Node}" ItemsSource="{Binding ChildNodes}">
<
Grid Width="100" Height="200"><
TextBlock Text="{Binding Text}" /></
Grid></
HierarchicalDataTemplate></
ResourceDictionary></
TreeView.Resources><!--
Put the root item(s) in a centered Grid so thatthey will be centered and retain their width.
--><
TreeView.ItemsPanel><
ItemsPanelTemplate><!--1-->
<
Grid Background="Blue" HorizontalAlignment="Stretch" IsItemsHost="True"></
Grid></
ItemsPanelTemplate></
TreeView.ItemsPanel></
TreeView></
Window>
TreeViewItem's template:
<
ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!--
This Style redefines the ControlTemplate used by TreeViewItems andalso provides a different itemspanel for their child items.
--><
Style TargetType="TreeViewItem"><
Setter Property="Template"><
Setter.Value><
ControlTemplate TargetType="TreeViewItem"><
Grid Background="Gray" Canvas.Left="{Binding Path=X}" Canvas.Top="{Binding Path=Y}"><!--
This Border and ContentPresenter displays the content of the TreeViewItem. --><
Grid Name="Bd" Margin ="10" Background="LightGray"><
ContentPresenter Name="PART_Header" ContentSource="Header"/>
<
ItemsPresenter/>
</
Grid><!--
The ItemsPresenter displays the item's children. --></
Grid></
ControlTemplate></
Setter.Value></
Setter><!--
Make each TreeViewItem show it's childrenin a horizontal StackPanel.
--><
Setter Property="ItemsPanel"><
Setter.Value><
ItemsPanelTemplate><
Grid Background="Green" Opacity="0.2" IsItemsHost="True" Margin="10,10"></
Grid></
ItemsPanelTemplate></
Setter.Value></
Setter></
Style></
ResourceDictionary>
I think the misunderstanding here is with Grid. Grid does not work well as an ItemsPresenter since it will not automatically generate rows or columns for you and will not set the row or column incrementally for each child. Try using a StackPanel instead to get things started and working before trying to get the centering described in the comments.
Since you seem to be getting a root, I'll assume that the data is being hooked up somewhere, but from the XAML, I don't immediately see where the data from the resource dictionary is being applied as the items or ItemsSource for the TreeView. Maybe I'm just not seeing it, though.
Ben
Suppose you have this example:
You will see that "Example" and "String" overlap each other. If you are not seeing this behavior, then there is code or XAML that is changing some aspect of this.
Ben