WPF TreeView树节点双击实现编辑重命名,或右菜单重命名怎么实现?
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>();
}
}