how to pass data when using MenuItem.ItemContainerStyle
Posted
by black sensei
on Stack Overflow
See other posts from Stack Overflow
or by black sensei
Published on 2010-05-17T13:09:55Z
Indexed on
2010/05/17
13:40 UTC
Read the original article
Hit count: 313
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
© Stack Overflow or respective owner