使用Treeview控件进行数据绑定 [英] databinding with treeview control
问题描述
我在我的一个应用程序上使用c#wpf .net framework 3.5
Hi, i am using c# wpf .net framework 3.5 on one of my apps
我正在尝试在运行时在Treeview控件中填充分层树
I am tring to populate a hierarchical tree at runtime in a treeview control
让我们以以下示例为例:
Lets take the following for example:
private void taxonomy_populate_click(object sender, RoutedEventArgs e)
{
taxonomytree.Items.Clear();
treeviewitem tviI = new treeviewitem() { Header = "I", tid = 1 };
treeviewitem tviIA = new treeviewitem() { Header = "I.A", tid = 2 };
treeviewitem tviIA1 = new treeviewitem() { Header = "I.A.1", tid = 3 };
treeviewitem tviIA2 = new treeviewitem() { Header = "I.A.2", tid = 4 };
treeviewitem tviIB = new treeviewitem() { Header = "I.B", tid = 5 };
tviI.Items.Add(tviIA);
tviIA.Items.Add(tviIA1);
tviIA.Items.Add(tviIA2);
tviI.Items.Add(tviIB);
taxonomytree.Items.Add(tviI);
//TreeViewItem ti = new TreeViewItem() { Header = "HI" };
//this.taxonomytree.Items.Add(ti);
}
我举了这个例子,因为如果我可以使其与这段代码一起工作,那么我可以为了方便起见将代码片段更改为外观
I took this example because if i can make it work with this code then i can change the code fragment to a look for my convenience
现在我有一个树状视图
我希望树形视图控件显示数据集中的数据,如下所示:
i want the tree view control to display data from a dataset, something like the following:
public class Taxonomy
{
private string _name;
private string _tid;
public string Tid
{
get { return _tid; }
set { _tid = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public Taxonomy(string name, string tid)
{
Name = name;
Tid = tid;
}
}
此处的名称"将是在程序中显示的那个,并且将"tid"显示在程序中.是保留在背景中的东西,当有人单击树视图中的特定节点时将激活并使用
here the "name" would be the one displayed in the program and "tid" is something kept in background that will be activated and used when someone clicks on a particular node in the tree view
在分配Treeview控件的属性以及绑定数据时遇到问题
I am having problems in assigning the treeview control the properties and in binding the data
有人可以帮我实现这一目标
can some one help me how to achieve this
例如,在上一个示例中,我能够使用:p来实现ListView控件的相同功能:
for example in a previous example i was able to achieve the same for a ListView control using :
<ListView ItemsSource="{Binding UserCollection}" SelectedItem="{Binding SelectedUserInfo}" >
然后在主程序中绑定Usercollection和SelectedUserInfo
and then bind the Usercollection and SelectedUserInfo in the main program
还有一件事,我想使用c#而不是xaml来实现
One more thing , i want to use c# and not xaml to achieve
请帮助!
推荐答案
确定
这就是陷阱
我能够在以下事件处理程序的帮助下填充树视图(实际上是我想要的一个按钮):
i am able to populate the treeview with the help of following eventhandler (a button actually, which is what i want):
private void taxonomy_populate_click(object sender, RoutedEventArgs e)
{
taxonomytree.Items.Clear();
treeviewitem tviI = new treeviewitem() { Header = "I", tid = 1 };
treeviewitem tviIA = new treeviewitem() { Header = "I.A", tid = 2 };
treeviewitem tviIA1 = new treeviewitem() { Header = "I.A.1", tid = 3 };
treeviewitem tviIA2 = new treeviewitem() { Header = "I.A.2", tid = 4 };
treeviewitem tviIB = new treeviewitem() { Header = "I.B", tid = 5 };
tviI.Items.Add(tviIA);
tviIA.Items.Add(tviIA1);
tviIA.Items.Add(tviIA2);
tviI.Items.Add(tviIB);
taxonomytree.Items.Add(tviI);
}
我已经创建了一个上下文菜单,如下所示:
and i have made a context menu, something like this:
private void Delete_taxonomy(object sender, RoutedEventArgs e)
{
try {
Taxonomy taxo = taxonomytree.SelectedItem as Taxonomy;
if (taxo != null)
{
System.Windows.Forms.MessageBox.Show("" + taxo.Tid);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(""+ex.Message);
}
}
在这里,我向每个对象发送两个属性:标头和tid
Here i am sending each object with two properties : Header and a tid
我希望标题显示在最终用户的树状视图中,当他单击特定的上下文菜单时,必须分别使用该标题的提示
I want the header to be displayed to the end user in the treeview and when he click a particular context menu the tid must be used respective of that header
这可能吗
请帮助
这篇关于使用Treeview控件进行数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!