使用模板HierarchicalDataTemplate
<HierarchicalDataTemplate x:Key="BookMarkTemplate" > <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Child.MarkName,Mode=TwoWay}"></TextBlock> </StackPanel> </HierarchicalDataTemplate> <HierarchicalDataTemplate x:Key="ChapterTemplate" ItemTemplate="{StaticResource BookMarkTemplate}" ItemsSource="{Binding InlineList,Mode=TwoWay}" > <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Name}"></TextBlock> </StackPanel> </HierarchicalDataTemplate>
从父节点删除选中项
<TreeView x:Name="treeview" TreeViewItem.Selected="treeView1_Selected" ItemTemplate="{StaticResource ChapterTemplate}">
private void treeView1_Selected(object sender, RoutedEventArgs e) { if ((e.OriginalSource as TreeViewItem).Header.GetType()==typeof(InlineUIContainer)) { mark = ((e.OriginalSource as TreeViewItem).Header as InlineUIContainer); BookMarkRun run = mark.Child as BookMarkRun; txtSelectionContent.Text = run.MarkName; DependencyObject parent = VisualTreeHelper.GetParent((e.OriginalSource as TreeViewItem)); while (!(parent is TreeViewItem)) parent = VisualTreeHelper.GetParent(parent); TreeViewItem item = (TreeViewItem)parent; volumeModel = (item.Header as VolumeModel); btnAdd.Content = "修改"; btnDel.IsEnabled = true; btnAdd.IsEnabled = true; } }