我一直试图让一个动态的ContextMenu
显示其collection of objects
中的每个对象的名称属性。
这里是一个具体的例子,我连接到webservice
拉contacts
和groups
一个特定的帐户。所以我有那些作为global variables
。 i display the contacts in a listbox
和i want to show on right click of a contact in the listbox the list of groups that it can be added to
。
能够添加联系人到一个组我需要联系人(我有)的id和我在这里寻找的组的id是我的代码。如何使用MenuItem.ItemContainerStyle时传递数据
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>
......
和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();
这段代码显示了群体好吗的名字,但我需要的组ID点击。
如果你已经注意到我评论了一部分xaml代码。有,我可以绑定(轻松)的ID给tag.But它不会工作和MenuItem.ItemContainerStyle是一个工作,但随后我迷路了:
问题1:如何创建对于具有组的名称的子菜单的点击事件的处理方法?
问题2:我如何获得点击的组ID以使用?
感谢您的阅读并善意帮助我在此
感谢的作品 – 2010-05-17 15:31:22