Multiple Context Menus in a HierachicalDataTemplate

May 17, 2011 at 10:30 PM

Hi there, I'm working on a project which has departments, groups in those departments and users in those groups.
And like most here we wanted a right click feature for our Silver Light app.

I'm still fairly new to silver light and wpf in general and I'm having a few issues figuring out your DLL. I'm attempting to have a separate PopupMenu for each section of my tree view; one for users, one for groups, one for departments. I currently have a setup for group and user, but when ever I click on user I get the user and the group context. I understand why, since its just within the same bubble, I just can't figure out how to prevent it. I'm also wondering if I can use the GetClickedElement() to save myself from having to design a way to figure out the selected item of the treeView.

The only odd thing possibly at first glance would be the

Header="{Binding Path=DepartmentFolderTreeViewText}

We have a group and User one as well but only the department is currently databound, this is because we wanted a folder type of header so we can localize something for another language.

Hopefully you or someone else can explain wpf and your DLL a bit more to me, if not I'm sure enough poking will reveal results. If you have any more questions just ask.

Here's the main xaml

                <sdk:HierarchicalDataTemplate x:Key="UsersTemplate">
                        <TextBlock x:Name="UserItem" Text="{Binding user.userName}" />
                        <rclick:PopupMenu x:Name="UserItemMenu" RightClickElements="UserItem" >
                                <rclick:PopupMenuItem Header="EditUser" Click="EditUser_Click" />
                                <rclick:PopupMenuItem Header="DeleteUser" Click="DelUser_Click"/>
                <sdk:HierarchicalDataTemplate x:Key="GroupsTemplate" >
                        <sdk:TreeViewItem x:Name="GroupItem" Header="{Binding Path=groupName}">
                            <sdk:TreeViewItem Header="Users" ItemsSource="{Binding Path=groups_users}" ItemTemplate="{StaticResource UsersTemplate}" />
                        <rclick:PopupMenu x:Name="GroupItemMenu" RightClickElements="GroupItem">
                                <rclick:PopupMenuItem Header="EditGroup" Click="EditGroup_Click"/>
                                <rclick:PopupMenuItem Header="DeleteGroup" Click="DelGroup_Click"/>
                <sdk:HierarchicalDataTemplate x:Name="DTTest" x:Key="DepartmentsTemplate" >
                    <sdk:TreeViewItem Header="{Binding Path=departmentName}" >
                        <sdk:TreeViewItem x:Name="GroupHeader" Header="Groups" ItemsSource="{Binding Path=groups}" ItemTemplate="{StaticResource GroupsTemplate}" />

            <sdk:TreeView  x:Name="DepartmentTreeView" MinWidth="200" Height="300" HorizontalAlignment="Left" VerticalAlignment="Stretch" >
                <sdk:TreeViewItem x:Name="DepartmentHeader" Header="{Binding Path=DepartmentFolderTreeViewText}" ItemsSource="{Binding Path=MyDepartments}"  ItemTemplate="{StaticResource DepartmentsTemplate}" />


May 21, 2011 at 12:13 AM
Edited May 21, 2011 at 12:33 AM

I guess the easiest way for this would be to use a single PopupMenu for all items in the Treeview.

You would then use GetClickedElement() in the Opening event to determine which item was clicked.

From there you can either:

1. Generate the menu programmatically(like in Demo1).

2. Choose which Listbox you want to display(assuming they are predefined in XAML) via the menu's ItemsControl property.

But I'm assuming you're ok with using code here.

Does that answer your question?