积极答复者
WPF 关于通过DataSet绑定到TreeView的例子

问题
答案
-
参考
http://stackoverflow.com/questions/14011366/binding-treeview-to-dataset
<TreeView Name="tv" ItemsSource="{Binding Root}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Parents}">
<TextBlock Text="{Binding Value}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>DataSet ds = new DataSet("Data");
DataTable t = new DataTable("Table");
t.PrimaryKey = new DataColumn[] { t.Columns.Add("Key", typeof(int)) };
t.Columns.Add("Value", typeof(string));
t.Columns.Add("Father", typeof(int));
ds.Tables.Add(t);
ds.Relations.Add("Parents", t.Columns["Key"], t.Columns["Father"], false);
t.Rows.Add(1, "First", -1);
t.Rows.Add(2, "Second", -1);
t.Rows.Add(3, "Third", 2);
Root = new DataView(t);
Root.RowFilter = "[Father]<0";
t.DefaultView.RowFilter = "[Father] < 0";
tv.DataContext = this;专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
- 已标记为答案 JaremyShort 2016年6月21日 3:37
全部回复
-
参考
http://stackoverflow.com/questions/14011366/binding-treeview-to-dataset
<TreeView Name="tv" ItemsSource="{Binding Root}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Parents}">
<TextBlock Text="{Binding Value}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>DataSet ds = new DataSet("Data");
DataTable t = new DataTable("Table");
t.PrimaryKey = new DataColumn[] { t.Columns.Add("Key", typeof(int)) };
t.Columns.Add("Value", typeof(string));
t.Columns.Add("Father", typeof(int));
ds.Tables.Add(t);
ds.Relations.Add("Parents", t.Columns["Key"], t.Columns["Father"], false);
t.Rows.Add(1, "First", -1);
t.Rows.Add(2, "Second", -1);
t.Rows.Add(3, "Third", 2);
Root = new DataView(t);
Root.RowFilter = "[Father]<0";
t.DefaultView.RowFilter = "[Father] < 0";
tv.DataContext = this;专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
- 已标记为答案 JaremyShort 2016年6月21日 3:37
-
你好, Shuanghua Li
我已经看了参考文章,但是我不明白一张表建立DataRelation是什么意思,同时我还想知道 这种方式建立的关联以及XAML中有什么问题
-
你好, Shuanghua Li
对的,是两张表,两张表不能使用这种关联方式吗?