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

zhaowei303 zhaowei303 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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>();
}
}
...全文

相关推荐

wpf mvvm treeview 动态加载(实现重命名与右键添加)

综合网上资源制作的,根据那些零碎的代码组合的,高手请跳过。已经整理好,可以运行。(特别注意,只有第一个根目录树加载了右键命令,第二个没有加载,自己可以根据需要改变每个树的右键命令)

akwkevin

WPF TreeView 数据绑定完全指南:MVVM 模式实现

分层结构设计:将业务逻辑、数据模型和视图清晰分离双向绑定:及时同步 UI 状态与数据模型异步加载:大型树结构使用延迟加载提升性能虚拟化支持:处理大量节点时开启 UI 虚拟化命令模式:使用 ICommand 实现 UI 操作解耦模板选择器:复杂场景下可使用 TemplateSelector 实现动态模板切换通过以上实现方法和最佳实践,您可以创建出响应式、可维护的树形界面,充分发挥 WPF 数据绑定的强大功能。

若汝棋茗

WPF TreeView1 双击新增节点 到TreeView2中,右键菜单删除TreeView2子节点

WPF TreeView1 双击新增节点 到TreeView2中,选中TreeView2子节点 右键删除子节点

松簸箩

WPF Treeview 点击右键弹出菜单,同时鼠标点击处的TreeViewItem被选中

有时候我们需要在TreeView中实现这样的功能: 在TreeView上点击右键弹出菜单,同时鼠标点击处的TreeViewItem被选中,然后我们针对选中的数据进行处理。 不过,WPF的TreeView并没有提供右键单击选中的功能。我们需要自己去实现。 【思路】 最基本的思路是,在TreeView右键点击的事件发生时,我们遍历它所有的Item,包括子Item,获取Item所对应的TreeViewItem控件的边界Rect,然后调用Rect的Contains方法判断鼠标是否在该范围内。(

tianzhiguo

WPF MVVM模式下 TreeView 右键菜单

WPF MVVM模式下TreeView右键选中项,右键菜单 博客地址:http://www.cnblogs.com/xcong/

LSP学逆向
评论 6
收藏 已收藏
分享
返回顶部
zhaowei303
WPF/Silverlight 8,757 成员 12,031 内容