简体   繁体   中英

Create custom tree view with a dynamic context menu C# WPF

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

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM