TreeView:如何以编程方式将新的Child节点添加到WPF C中的选定节点# [英] TreeView: How do I programmatically add a new Child node to a Selected Node in WPF C#
问题描述
我有一个树视图,我需要在选择另一个节点时添加新的子节点。我有一个按钮,我需要允许用户在按下该按钮时将新的子节点添加到选定的节点。我怎样才能做到这一点好人呢? :)
我也有一个OnItemSelected方法:
I have a treeview to which I need to add new child nodes when another node is selected. I have a button and I need to allow the user to add a new child node to a selected node when that button is pressed. How do I get that done good folks? :)
I have a OnItemSelected method too:
private void TreeViewItem_OnItemSelected(object sender, RoutedEventArgs e)
{
nameTxt.Text = e.Source.ToString();
}
推荐答案
请参阅:
https://msdn.microsoft.com/en-us/library/system.windows。 controls.treeviewitem%28v = vs.110%29.aspx [ ^ ],
https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol。 items%28v = vs.110%29.aspx [ ^ ],
https://msdn.microsoft.com/en-us/library/system .windows.controls.itemcollection%28v = vs.110%29.aspx [ ^ ],
https://msdn.microsoft.com/en-us/library/ms589116(v = vs.110)的.aspx [ ^ ]。
现在,让我们按照这些帮助页面进行操作。从头开始创建WPF应用程序并将一些示例代码添加到主窗口。这是一个完整的文件:
Please see:
https://msdn.microsoft.com/en-us/library/system.windows.controls.treeviewitem%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.itemcollection%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/ms589116(v=vs.110).aspx[^].
Now, let's follow these help pages. Create a WPF application from scratch and add some sample code to main window. This is a complete file:
using System.Windows;
using System.Windows.Controls;
namespace SampleApplication.Ui {
public partial class MainWindow : Window {
public MainWindow() {
// InitializeComponent(); // not really needed,
// because this sample is fully programmatical;
// you don't even need XAML if you properly write
// your entry-point method (Main)
TreeView treeView = new TreeView();
TreeViewItem top = new TreeViewItem();
top.Header = "sample header";
TreeViewItem child = new TreeViewItem();
child.Header = "sample child";
TreeViewItem grandchild = new TreeViewItem();
grandchild.Header = "sample grandchild";
treeView.Items.Add(top);
top.Items.Add(child);
child.Items.Add(grandchild);
this.AddChild(treeView);
} //MainWindow
} //class MainWindow
} //namespace SampleApplication.Ui
但实际上,这不是很典型的用法。在大多数情况下,您需要学习数据绑定。例如,请参阅 http://www.wpf-tutorial.com/treeview -control / treeview-data-binding-multiple-templates [ ^ ]。
另请参阅此更高级的CodeProject文章:使用ViewModel模式简化WPF TreeView [ ^ ]。
In practice, however, this is not very typical usage. In most cases, you need to learn data binding. See, for example, http://www.wpf-tutorial.com/treeview-control/treeview-data-binding-multiple-templates[^].
See also this more advanced CodeProject article: Simplifying the WPF TreeView by Using the ViewModel Pattern[^].
这篇关于TreeView:如何以编程方式将新的Child节点添加到WPF C中的选定节点#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!