我使用层次结构数据模板填充了我的树视图,并使用子菜单项的数据模板。
TreeViewItem item = (TreeViewItem)this.view.ItemContainerGenerator.ContainerFromIndex(0);
我正在努力在父树状项目(项目)中获取treeviewitems。
我尝试了ItemContainerGenerator,但以下内容返回null。
TreeViewItem child = (TreeViewItem)item.ItemContainerGenerator.ContainerFromIndex(0);
当我使用VisualTreeHelper.GetChild()方法遍历treeviewitem时,我最终得到了包含子treeviewitems的 ItemsPresenter 的null返回。
有什么方法可以在treeviewitem中访问子treeviewitems吗?
模板的xaml代码:
<DataTemplate x:Key="Navigation_SubDataTemplate">
<StackPanel Orientation="Horizontal">
<ContentControl Focusable="False" Background="{DynamicResource navigationlistboxfont}" Template="{Binding MenuTemplate}" Margin="5" Width="16" Height="16"/>
<TextBlock Text="{Binding Path=MenuName}" ToolTip="{Binding ToolTip}" FontFamily="Segoe UI Light" FontSize="16" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
<HierarchicalDataTemplate x:Key="Navigation_DataTemplate" ItemsSource="{Binding SubMenuItems}" ItemTemplate="{StaticResource Navigation_SubDataTemplate}">
<StackPanel Orientation="Horizontal">
<ContentControl Focusable="False" Background="{DynamicResource navigationlistboxfont}" Template="{Binding MenuTemplate}" Margin="5" Width="20" Height="20"/>
<TextBlock Margin="4,2" Text="{Binding Path=MenuName}" ToolTip="{Binding ToolTip}" FontFamily="Segoe UI Light" FontSize="16" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center">
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
我的树视图
<TreeView x:Name="MytreeView" helper:TreeViewExtension.SelectedItem="{Binding ViewSelected, Mode=TwoWay}" ItemsSource="{Binding ViewMenuItems}" ItemTemplate="{DynamicResource Navigation_DataTemplate}" Background="{x:Null}" BorderBrush="{x:Null}" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" Margin="0" ItemContainerStyle="{DynamicResource TreeViewItemStyle1}" />
这是我用于绑定到treeview的viewmenuitem的c#代码
List<ViewMenu> subMenus = new List<ViewMenu>();
subMenus.Add(new ViewMenu() { MenuName = "Child1" });
subMenus.Add(new ViewMenu() { MenuName = "Child2" });
ViewMenuItems = new ObservableCollection<ViewMenu>();
ViewMenuItems.Add(new ViewMenu() { MenuName = "Parent", SubMenuItems = subMenus });
答案 0 :(得分:1)
以下方法将检索TreeViewItem的所有直接子TreeViewItem对象。
List<TreeViewItem> GetChildren(TreeViewItem parent)
{
List<TreeViewItem> children = new List<TreeViewItem>();
if (parent != null)
{
foreach (var item in parent.Items)
{
TreeViewItem child = item as TreeViewItem;
if (child == null)
{
child = parent.ItemContainerGenerator.ContainerFromItem(child) as TreeViewItem;
}
children.Add(child);
}
}
return children;
}
答案 1 :(得分:0)
@Glen Thomas 的回答在您的树视图项是 TreeViewItem 类型时有效。就我而言,我将树视图的源设置为 XDocument。如果您将代码更改为:
List<TreeViewItem> GetChildren(TreeViewItem parent)
{
List<TreeViewItem> children = new List<TreeViewItem>();
if (parent != null)
{
foreach (var item in parent.Items)
{
TreeViewItem child = item as TreeViewItem;
if (child == null)
{
child = parent.ItemContainerGenerator.ContainerFromItem(parent) as TreeViewItem;
}
children.Add(child);
}
}
return children;
}
它可以找到任何类型的元素。 之前是:
if (child == null)
{
child = parent.ItemContainerGenerator.ContainerFromItem(child) as TreeViewItem;
}
这没有意义,因为如果 child 为 null,显然 ContainerFromItem(null) 将返回 null,使 child 为 null。