我一直試圖讓一個動態的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