How to make gridview a child of a tree in a wpf application

I am trying to populate a data grid (or grid view) as a child of a tree in the database. I can get data from a database in a tree, however, this does not seem to work with a data grid. Here is my xaml code:

<Window x:Class="AttemptUsingHirarchichalData.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:data="clr-namespace:AttemptUsingHirarchichalData" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" Title="Window1" Height="300" Width="300"> <Window.Resources> <HierarchicalDataTemplate DataType="{x:Type data:Root}" ItemsSource="{Binding Path=RootList}"> <TextBlock Text="{Binding RootNode}"/> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type data:Nodes}" ItemsSource="{Binding Path=ChildList}"> <TextBlock Text="{Binding ChildNode}"/> </HierarchicalDataTemplate> </Window.Resources> <Grid> <TreeView Name="TreeView1"> <TreeViewItem ItemsSource="{Binding Path=RootList}" Header="{Binding RootNode}"/> <TreeViewItem ItemsSource="{Binding Path=dt_Age}" Header="{Binding dt_Age}"/> </TreeView> </Grid> 

My code is something like this:

 InitializeComponent(); Root obj_Root = new Root(); obj_Root.RootNode = "RootNode"; obj_Root.RootList = new List<Nodes>(); Class1 obj_Class1 = new Class1(); DataTable dt_Age = obj_Class1.GetAgeInComboBox(); for (int i = 0; i < dt_Age.Rows.Count; i++) { Nodes obj_AgeNode = new Nodes(); obj_AgeNode.ChildNode = dt_Age.Rows[i][0].ToString(); obj_Root.RootList.Add(obj_AgeNode); Class1 obj_class = new Class1(); DataTable dt_name = new DataTable(); dt_name = obj_class.GetName(Convert.ToInt32(dt_Age.Rows[i][0])); obj_AgeNode.ChildList = new List<Nodes>(); //gridv for (int j = 0; j < dt_name.Rows.Count; j++) { Nodes obj_NameNode = new Nodes(); obj_NameNode.ChildNode = dt_name.Rows[j][0].ToString(); obj_AgeNode.ChildList.Add(obj_NameNode); } } TreeView1.DataContext = obj_Root; 

My class file has this as part of this:

 public class Nodes { public string ChildNode { get; set; } public List<Nodes> ChildList { get; set; } } public class Root { public string RootNode { get; set; } public List<Nodes> RootList { get; set; } } public DataTable GetAgeInComboBox() { SqlDataAdapter da_Age = new SqlDataAdapter("select distinct Age from myfrstattemt", conn1); DataTable dt_Age = new DataTable(); da_Age.Fill(dt_Age); return dt_Age; } 

Please tell me how to implement this. I am new to this, so please excuse my stupid mistakes, and please try to explain in simple terms. Thanks.

This is what I really need to do.

+3
source share
2 answers

The good news is that you do a lot more work here than you need, which is probably due to the fact that you have problems.

The bad news is that you have to really learn a little more about WPF in order to understand this correctly and come up with a good approach, clean and concise. I will try to point you in the right direction.

First, you have to break away from the ItemsControl. This is a really powerful class and is the base class of many everyday controls that you will use in your WPF application. You should understand how binding any collection (IEnumerable, IList, IBindingList, etc.) to the ItemsSource property of the ItemsControl element will lead to the creation of child elements.

Then you should understand (if you don't already know this) how data types are converted to user interface elements through DataTemplates. This is a simple but powerful concept.

Then you should experiment with the small extension mentioned above, namely HeaderedItemsControl and HierarchicalDataTemplate. This will give you all the tools you need to use TreeView in the form you want.

In no case do you need to create any TreeViewItems in C # code. If you can get the underlying data objects to display the hierarchy that you want to display (regardless of whether each node is a simple text label or a data network), you can create hierarchical data templates for all levels and support WPF by linking everything and creating TreeViewItems for you .

EDIT

I have questions about your edited question:

  • What is the difference between Root and Nodes ?
  • Do you have a class hierarchy that models the relationship between nodes? If so, just use it, and not copy objects to Root and Nodes instances. I will give you an example.

Suppose you have Customer that put Order s, and each order has Item s.

 public class Customer { public string Name { get; set; } public IEnumerable<Order> Orders { get; set; } } public class Order { public DateTime PurchaseDate { get; set; } public IEnumerable<OrderItem> Items { get; set; } } public class OrderItem { public string ProductName { get; set; } public int Quantity { get; set; } public double UnitPrice { get; set; } public double TotalPrice { get; set; } } 

The above types represent a hierarchy. If you have such a structure, you can bind it directly to the user interface. You do not need to create Root or Node objects. This is the WPF way :)

(Note that if you do not have the above class hierarchy, you can customize it specifically for use in the user interface. Learn more about the MVVM template if you are interested.)

In your XAML, you define a TreeView as:

 <TreeView x:Name="_treeView" ItemsSource="{Binding}"> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type data:Customer}" ItemsSource="{Binding Path=Orders}"> <TextBlock Text="{Binding Name}"/> </HierarchicalDataTemplate> <DataTemplate DataType="{x:Type data:Order}"> <StackPanel> <TextBlock Text="{Binding PurchaseDate}"/> <ListView ItemsSource="{Binding Items}"> <ListView.View> <GridView> <GridViewColumn DisplayMemberBinding="{Binding ProductName}" /> <GridViewColumn DisplayMemberBinding="{Binding Quantity}" /> <GridViewColumn DisplayMemberBinding="{Binding UnitPrice}" /> <GridViewColumn DisplayMemberBinding="{Binding TotalPrice}" /> </GridView> </ListView.View> </ListView> </StackPanel> </DataTemplate> </TreeView.Resources> </TreeView> 

And in encoding, you would do something like this:

  _treeView.DataContext = customers; // eg. IEnumerable<Customer> 
+11
source

Maybe you should take a look at this post from Marlon Grech.

0
source

Source: https://habr.com/ru/post/971752/


All Articles