0

I want to create a dynamic tree view from a list in C# WPF. So I will get all the queues (queue1, ...) and topics (topic1, ...) from a list. Furthermore I need a specific context menu for the different hierarchical points. I want to create a tree view like this:

queues

  • queue1
  • queue2

topics

  • topic1
  • topic2

There should be a specific context menu for the main point queues and a specific for the main point topics. Additional I need a specific for the subitems queues1 and the topic1. I tried a few things but without success. Has anybody a small example which shows who to solve this problem?

Best Regards

2 Answers 2

1

Creating a tree is not an issue. It involves bit of work, but straight forward. You have to create a hierarchical data template from your list and get the tree populated. Following link has all the info.

Creating a wpf tree

OR if you don't want to use sdk

page resource:

tree in xaml:

<Grid TextElement.FontSize="10" DataContext="{StaticResource MyHierarchicalViewSource}" >
<GroupBox x:Name="gbTree">
<TreeView Name="HierarchyTreeview" HorizontalAlignment="Left" AllowDrop="True"
          BorderThickness="0" VerticalAlignment="Top" Height="Auto" 
                              ItemsSource="{Binding}">
 <TreeView.ItemTemplate>
   <HierarchicalDataTemplate ItemsSource="{Binding Itemchildren, Mode=TwoWay}">
      <StackPanel Orientation="Horizontal" Margin="2">

        <TextBlock x:Name="text" Text="{Binding Item.ItemLabel}" >
        </TextBlock>

     </StackPanel>
     </HierarchicalDataTemplate>
   </TreeView.ItemTemplate>
  </TreeView> 
 </GroupBox>
</Grid>

code behing:

    Me._HierarchyViewSource = CType(Me.Resources("MyHierarchicalViewSource"), System.Windows.Data.CollectionViewSource)
Me._HierarchyViewSource.Source = your hierarchical data collection

assuming your hierarchy class structure:

Item has
 ItemChildren collection

However,I have the same issue in creating a specific context menu. I have posted my own question and haven't found a solution yet. I tried to do it with data triggers with no luck. The only way I know is creating a context menu for the whole tree and make it visible or invisible depending on item type.

If I find a workaround, I will post.

Sign up to request clarification or add additional context in comments.

2 Comments

hy, I asked the same queuestion in a the microsoft forum and I got an answer. Maybe it helps you. social.msdn.microsoft.com/Forums/vstudio/en-US/… Best regards
Thanks for that. Glad if I could help you in some way
0

I used the following code for add to TreeView dynamically.

CategorieList - Collection of categories which has id, name, bool value as IsSubCategory and Id of parent category.

private void AddToTree()
        {
            List<Category> topCategory = CategorieList.Where(c => c.IsSubCategory == false).ToList();

            foreach(Category c in topCategory)
            {
                CategoryTree.Items.Add(CreateTreeViewItem(c));
            }
        }

    private TreeViewItem CreateTreeViewItem(Category category)
    {
        TreeViewItem tItem = new TreeViewItem();
        tItem.Header = category.Name;
        List<Category> sub = CategorieList.Where(c => category.Id == c.ParentCategory).ToList();
        if (null != sub && sub.Count() != 0)
        {
            foreach (Category c in sub)
            {
                TreeViewItem item = CreateTreeViewItem(c);
                tItem.Items.Add(item);
            }
        }

        return tItem;
    }

XAML code is below

<TreeView x:Name="CategoryTree"> </TreeView>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.