O
O
oleg_ka2016-10-31 22:41:38
SQL
oleg_ka, 2016-10-31 22:41:38

How to display table from DB in TreeView WPF?

There is a hierarchical table shown in the figure.
ccf3f7ae8276460a9e8d7ac8e714ca1e.jpg
There is a connection to the database.
How to display this table in TreeView?
-Guitar
-----Acoustic
------------Classical
------------ 6-string(metal strings)
-----Bass
--- -------- Acoustic
------------ 4-string
----- Keyboard
------------ Synth
------- ----- midi keyboard

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sumor, 2016-11-01
@oleg_ka

You need to create a class like this:

class Item
{
  public string Name {get;set;}
  public IEnumerable<Item> Children 
  {
    get {/* тут код получения из базы подчинённых элементов*/}
  }
}

Get a list of top-level items and assign the TreeView's ItemsSource.
In TreeView, in the description of the template for the element (ItemTemplate), describe the HierarchicalDataTemplate and specify the property for obtaining child elements in ItemsSource={Binding Children}.
<TreeView>
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
      <TextBlock Text="{Binding Name}" />
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>

A
AndNovak, 2016-12-29
@AndNovak

Can we see your solution?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question