首页 新闻 会员 周边

WPF中TreeView添加节点的问题

0
悬赏园豆:50 [待解决问题]

最近在用WPF做东西,遇到个问题困了我好久都搞不定,只好来求大神赐教,用HierarchicalDataTemplate作为显示TreeView节点的模板,程序可以正常运行,我现在想实现的功能是:当选中一个TreeView节点,点击“添加”后,在所选节点的最后一个子节点之后添加一个TextBox,用来添加一个新的节点。这个功能类似于windows资源管理器中新建文件夹,这个功能在winform中只用new TreeViewItem(),然后调用TreeViewItem的Add()方法,把TextBox添加进TreeViewItem就行了,可是我现在是用HierarchicalDataTemplate作为TreeView节点的模板,这样的话,这个功能该如何实现???

windows资源管理器中的新建文件夹截图:

 

我的程序运行截图:

 

XAML代码如下:

<Window x:Class="WebSiteFavorite.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WebSiteFavorite"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" WindowStartupLocation="CenterScreen" Height="600" Width="800" Loaded="Window_Loaded">
    <Window.Resources>
        <HierarchicalDataTemplate x:Key="TreeViewTemplate" DataType="{x:Type local:PropertyNodeItem}" ItemsSource="{Binding 

Children}">
            <StackPanel>
                <Image/>
                <TextBlock Text="{Binding DisplayName}"/>
                <StackPanel.ToolTip>
                    <TextBlock Text="{Binding FileSystemInfo.FullName}"/>
                </StackPanel.ToolTip>
            </StackPanel>
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" MinWidth="50"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition MinWidth="100"/>
            </Grid.ColumnDefinitions>
            <DockPanel>
                <TreeView Name="tvFavorite" TreeViewItem.Selected="tvFavorite_Selected"  ItemTemplate="{StaticResource 

TreeViewTemplate}" FontSize="15">
                    <TreeView.ItemContainerStyle>
                        <Style TargetType="TreeViewItem">
                            <Setter Property="IsExpanded" Value="False"/>
                        </Style>
                    </TreeView.ItemContainerStyle>
                    <TreeView.ContextMenu>
                        <ContextMenu>
                            <MenuItem Name="tvcmAdd" Header="添加" Click="tvcmAdd_Click"/>
                            <MenuItem Name="tvcmDelete" Header="删除"/>
                        </ContextMenu>
                    </TreeView.ContextMenu>
                </TreeView>
            </DockPanel>
            <GridSplitter Grid.Column="1" HorizontalAlignment="Center" Width="5" Background="#BBB"/>            
        </Grid>
    </Grid>
</Window>

 

 

实体类PropertyNodeItem代码如下:

 1 public class PropertyNodeItem
 2     {
 3         string icon = "";
 4 
 5         public string Icon
 6         {
 7             get { return icon; }
 8             set
 9             {
10                 icon = value;
11             }
12         }
13 
14         string displayName = "";
15 
16         public string DisplayName
17         {
18             get { return displayName; }
19             set
20             {
21                 displayName = value;
22             }
23         }
24 
25         bool isExpanded = false;
26 
27         public bool IsExpanded
28         {
29             get { return isExpanded; }
30             set { isExpanded = value; }
31         }
32 
33         FileSystemInfo fileSystemInfo = null;
34 
35         public FileSystemInfo FileSystemInfo
36         {
37             get { return fileSystemInfo; }
38             set
39             {
40                 fileSystemInfo = value;
41             }
42         }
43 
44         private ObservableCollection<PropertyNodeItem> children = new ObservableCollection<PropertyNodeItem>();
45 
46         public ObservableCollection<PropertyNodeItem> Children
47         {
48             get { return children; }
49             set { children = value; }
50         }
51     }
52 }
欧阳.NET的主页 欧阳.NET | 初学一级 | 园豆:152
提问于:2014-05-28 20:06
< >
分享
所有回答(2)
0

这个如果是用的是ViewModel绑定的话,可能好解决一些,手上没有示例。大概意思就是:节点绑定到一个集合,添加时在后台给集合中增加一个空的Item,Xmal中可以使用trigger,来根据内容确定显示什么控件。

于为源 | 园豆:956 (小虾三级) | 2014-05-29 08:23
0

  关键是自定义节点,子節點集合

http://www.cnblogs.com/zizhijing/p/4184761.html

码杰 | 园豆:208 (菜鸟二级) | 2014-12-26 09:31
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册