WPF TreeView树节点双击实现编辑重命名,或右菜单重命名怎么实现?

zhaowei303 2014-03-11 11:40:34
WPF TreeView树节点双击实现编辑重命名,或F2键或右菜单重命名怎么实现呢?先谢谢了。

原始的TreeView实现代码:
<TreeView Name="tvProperties" Padding="0" Margin="0" BorderThickness="1" KeyDown="tvProperties_KeyDown">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<Image VerticalAlignment="Center" Source="{Binding Icon}" Width="16" Height="16" Margin="0,0,2,2"></Image>
<TextBlock VerticalAlignment="Center" Text="{Binding DisplayName}" PreviewMouseRightButtonDown="TextBlock_PreviewMouseRightButtonDown"></TextBlock>
<TextBox VerticalAlignment="Center" Text="{Binding DisplayName}" Visibility = "Visible"></TextBox>
<StackPanel.ToolTip>
<TextBlock VerticalAlignment="Center" Text="{Binding Name}" TextWrapping="Wrap" MaxWidth="200" ></TextBlock>
</StackPanel.ToolTip>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
后台代码:
private void ShowTreeView()
{
List<PropertyNodeItem> itemList = new List<PropertyNodeItem>();

PropertyNodeItem node1 = new PropertyNodeItem()
{
DisplayName = "Node No.1",
Name = "This is the discription of Node1. This is a folder.",
Icon = @"\Icons\CBook.gif",
};

PropertyNodeItem node1tag1 = new PropertyNodeItem()
{
DisplayName = "Tag No.1",
Name = "This is the discription of Tag 1. This is a tag.",
Icon = @"\Icons\CBook.gif",
EditIcon = @"\Icons\OBook.gif"
};
node1.Children.Add(node1tag1);

PropertyNodeItem node1tag2 = new PropertyNodeItem()
{
DisplayName = "Tag No.2",
Name = "This is the discription of Tag 2. This is a tag.",
Icon = @"\Icons\CBook.gif",
};
node1.Children.Add(node1tag2);
itemList.Add(node1);

PropertyNodeItem node2 = new PropertyNodeItem()
{
DisplayName = "Node No.2",
Name = "This is the discription of Node 2. This is a folder.",
Icon = @"\Icons\CBook.gif",
};

PropertyNodeItem node2tag3 = new PropertyNodeItem()
{
DisplayName = "Tag No.3",
Name = "This is the discription of Tag 3. This is a tag.",
Icon = @"\Icons\CBook.gif",
};
node2.Children.Add(node2tag3);

PropertyNodeItem node2tag4 = new PropertyNodeItem()
{
DisplayName = "Tag No.4",
Name = "This is the discription of Tag 4. This is a tag.",
Icon = @"\Icons\CBook.gif",
};
node2.Children.Add(node2tag4);
itemList.Add(node2);

this.tvProperties.ItemsSource = itemList;
}

private void tvProperties_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key ==Key.F2)
{
未实现代码
}

}


public class PropertyNodeItem
{
public string Icon { get; set; }
public string EditIcon { get; set; }
public string DisplayName { get; set; }
public string Name { get; set; }
public List<PropertyNodeItem> Children { get; set; }
public PropertyNodeItem()
{
Children = new List<PropertyNodeItem>();
}
}
...全文
1542 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyx100 2015-01-14
  • 打赏
  • 举报
回复
怎么解决的,楼主
西瓜巨人 2014-03-27
  • 打赏
  • 举报
回复
简单点的,你可以在双击的时候把被选中的那个TreeItem节点更换成TextBox ,替换原来处在该位置不可编辑的控件就可以了,编辑完成之后可以通过点击其他区域换回该节点为不可编辑控件
小恒丶 2014-03-12
  • 打赏
  • 举报
回复
引用 2 楼 zhaowei303 的回复:
[quote=引用 1 楼 u011182647 的回复:] 重命名你这样绑定你的数据源 然后更改就可以了 ObservableCollection<PropertyNodeItem> vaalists = new ObservableCollection<PropertyNodeItem>(); 右键菜单
 private void tvProperties_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            ListViewItem item = VisualUpwardSearch<ListViewItem>(e.OriginalSource as DependencyObject) as ListViewItem;
             item.Focus();
             if (item != null)
             {
                 item.ContextMenu = youjian();///自己写一个contextmenu
             }
        }
      static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(T))
                source = VisualTreeHelper.GetParent(source);

            return source;
        }

右键菜单非常感谢,重命名的话应该怎么操作呢。[/quote] 亲 不是给你说了嘛。重命名绑定的list类型换一下 然后直接获取selectitem 转换成你的list 然后修改就是了啊
zhaowei303 2014-03-12
  • 打赏
  • 举报
回复
引用 1 楼 u011182647 的回复:
重命名你这样绑定你的数据源 然后更改就可以了 ObservableCollection<PropertyNodeItem> vaalists = new ObservableCollection<PropertyNodeItem>(); 右键菜单
 private void tvProperties_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            ListViewItem item = VisualUpwardSearch<ListViewItem>(e.OriginalSource as DependencyObject) as ListViewItem;
             item.Focus();
             if (item != null)
             {
                 item.ContextMenu = youjian();///自己写一个contextmenu
             }
        }
      static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(T))
                source = VisualTreeHelper.GetParent(source);

            return source;
        }

右键菜单非常感谢,重命名的话应该怎么操作呢。
小恒丶 2014-03-12
  • 打赏
  • 举报
回复
重命名你这样绑定你的数据源 然后更改就可以了 ObservableCollection<PropertyNodeItem> vaalists = new ObservableCollection<PropertyNodeItem>(); 右键菜单
 private void tvProperties_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            ListViewItem item = VisualUpwardSearch<ListViewItem>(e.OriginalSource as DependencyObject) as ListViewItem;
             item.Focus();
             if (item != null)
             {
                 item.ContextMenu = youjian();///自己写一个contextmenu
             }
        }
      static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(T))
                source = VisualTreeHelper.GetParent(source);

            return source;
        }

8,756

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧