A
A
AndNovak2016-11-27 20:52:34
C++ / C#
AndNovak, 2016-11-27 20:52:34

How to implement Hierarchicaldatatemplate in caliburn micro?

Please tell me how to implement Hierarchicaldatatemplate in the TreeView element. I would like to add elements dynamically. The data will be loaded from the database.
In the ViewModel I receive data in this way

public MainViewModel(,ContractService contractService )
        {
            _contraDataService = contractService;
            _contraDataService.GetData(
            (items, error) =>
            {
                contract = items;
            });

        }

In View like this:
<TreeView>
        <TreeView.ItemTemplate>
             <HierarchicalDataTemplate ItemsSource="{Binding Path=contract}">
                    <HierarchicalDataTemplate.ItemTemplate>
                          <DataTemplate>
                               <TreeViewItem ItemsSource="{Binding Path=product}" />
                           </DataTemplate>
                      </HierarchicalDataTemplate.ItemTemplate>
              </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
 </TreeView>

As a result, the TreeView is not displayed at all. Please tell me where is my mistake.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2016-11-27
@Sumor

You probably forgot to set itemssource for Treeview itself.
After setting all the contracts in the ItemsSource, you will see them, and each will have the products from the product property.

<TreeView>
        <TreeView.ItemTemplate>
             <HierarchicalDataTemplate ItemsSource="{Binding Path=product}">
                    <TextBlock Text="{Binding} />
              </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
 </TreeView>

Thus, both contracts and products will be displayed in your Treeview.
If you want different templates for displaying each of the objects, you need to select them through the templateselector or, more simply, without specifying TreeView.ItemTemplate in the resources, set two typed templates, something like:
As for dynamic loading, by default, TreeView loads the entire data. And so you will load everything that will be reflected. If you want dynamic loading - read about virtualization. in relation to Treeview.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question