views:

237

answers:

1

Hello Experts!
i've been trying to have a dynamic ContextMenu to show the name property of each of the object in its collection of objects.
here is concrete example ,i'm connecting to a webservice to pull contacts and groups of a particular account.so i have those as global variables.i display the contacts in a listbox and i want to show on right click of a contact in the listbox the list of groups that it can be added to.
to be able to add a contact to a group i need the id of the contact(which i have) and the id of the group which i'm looking for here is my code.

xmlns:serviceAdmin="clr-namespace:MyWpfApp.serviceAdmin"
......
<ListBox.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Refresh" Click="RefreshContact_Click"></MenuItem> 
                            <MenuItem Header="Add New Contact" Click="ContactNew_Click"></MenuItem>
                            <MenuItem Header="Add to Group" Name="groupMenus">
                                //<!--<MenuItem.Resources>
                                  //  <DataTemplate DataType="{x:Type serviceAdmin:groupInfo}" x:Key="groupMenuKey" > 
                                     //   <MenuItem>
                                     //       <TextBlock Text="{Binding name}" />
                                     //   </MenuItem>
                                   // </DataTemplate>

                               // </MenuItem.Resources>-->
                                <MenuItem.ItemContainerStyle>
                                    <Style>
                                        <Setter Property="MenuItem.Header" Value="{Binding name}"/>
                                        <Setter Property="MenuItem.Tag" Value="{Binding id}" />

                                    </Style>
                                </MenuItem.ItemContainerStyle>
                            </MenuItem>
                            <MenuItem Header="Delete Selected" Click="ContactDelete_Click"></MenuItem>
                        </ContextMenu>
                    </ListBox.ContextMenu>
                    ......

and on xaml.cs

//this code is in the method that loads the groups
loadedgroup = service.getGroups(session.key, null);
 groupListBox.ItemsSource = loadedgroup;
 groupMenus.ItemsSource = loadedgroup.ToList();

this code is showing the name of the groups alright but i need the id of the group clicked on.
If you've noticed i commented a portion of the xaml code. with that i could bind(with ease) the id to the tag.But it won't work and the MenuItem.ItemContainerStyle is the one working but then i'm lost:

Question 1 : how do i create a handler method for a click event of a submenu that has the names of the groups?
Question 2 : how do i get the clicked group id to work with?

thanks for reading and kindly help me in this

+1  A: 

Below is a sample using data binding. The data context of a sub menu items is an instance of Group.

XAML:

<Window x:Class="MenuTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid Background="Red">
        <Grid.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Menu Item 1"></MenuItem>
                <MenuItem Header="Menu Item 2"></MenuItem>
                <MenuItem Header="SubMenu" ItemsSource="{Binding Path=Groups}"
                          Click="OnGroupMenuItemClick">
                    <MenuItem.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}" />
                        </DataTemplate>
                    </MenuItem.ItemTemplate>
                </MenuItem>
            </ContextMenu>
        </Grid.ContextMenu>
    </Grid>
</Window>

Code behind:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace MenuTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            Groups = new List<Group>();
            Groups.Add(new Group() { Name = "Group1", Id = 1 });
            Groups.Add(new Group() { Name = "Group2", Id = 2 });
            Groups.Add(new Group() { Name = "Group3", Id = 3 });

            DataContext = this;
        }

        public List<Group> Groups { get; set; }

        private void OnGroupMenuItemClick(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = e.OriginalSource as MenuItem;
            Group group = menuItem.DataContext as Group;
        }
    }

    public class Group
    {
        public string Name { get; set;}
        public int Id { get; set;}
    }
}
Wallstreet Programmer
thanks that works
black sensei