2010-12-19 81 views
4

我很難找到這個好的文檔,儘管尋找了一段時間。顯示一個ContextMenu

我想在我的應用程序上下文菜單複製看出與其他自來水和保持上下文菜單,就像從應用程序列表釘扎一個應用程序啓動屏幕的行爲。

這裏是我的上下文菜單:

   <toolkit:ContextMenuService.ContextMenu> 
        <toolkit:ContextMenu x:Name="sectionContextMenu"> 
         <toolkit:MenuItem Header="Hide this section from this list" /> 
        </toolkit:ContextMenu> 
       </toolkit:ContextMenuService.ContextMenu> 

我如何讓它顯示?

+0

簡單易用的方式來使用上下文菜單中的Windows Phone 7 http://www.akiievolution.com/context-menu-for-windows-phone-7-in-silverlight-toolkit/ – 2012-01-17 05:01:56

+0

檢查這篇文章:[WP7 ContextMenu深入|第一部分:關鍵概念和API(http://windowsphonegeek.com/articles/WP7-ContextMenu-in-depth--Part1-key-concepts-and-API)希望這會幫助你。 – 2010-12-21 16:09:23

回答

7

上下文菜單需要被附加到您希望用戶點擊並按住的元素。

<Border Margin="0,12" BorderBrush="{StaticResource PhoneForegroundBrush}" BorderThickness="2" Background="Transparent" VerticalAlignment="Center" Padding="16"> 
    <toolkit:ContextMenuService.ContextMenu> 
     <toolkit:ContextMenu x:Name="sectionContextMenu"> 
     <toolkit:MenuItem Header="Hide this section from this list" /> 
     </toolkit:ContextMenu> 
    </toolkit:ContextMenuService.ContextMenu> 
    <TextBlock Text="Tap and hold here to invoke a ContextMenu" Style="{StaticResource PhoneTextNormalStyle}"/> 
</Border> 

用戶現在可以用自來水調用右鍵菜單,並就這一Border元素的內容。

2

爲根據內容的不同項的唯一上下文菜單。

private ContextMenu CreateContextMenu(ListBoxItem lbi) 
{ 
    ContextMenu contextMenu = new ContextMenu(); 
    ContextMenuService.SetContextMenu(lbi, contextMenu); 
    contextMenu.Padding = new Thickness(0); 

    string item_1 = "item 1"; 
    if(lbi.Content is string) { 
     item_1 = lbi.Content as string; 
    } 
    contextMenu.ItemsSource = new List<string> { item_1, "item 2", "item 3" }; 
    contextMenu.IsOpen = true; 
    return contextMenu; 
} 

private void Results_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    if (Results.SelectedIndex == -1) return; 
    int index = Results.SelectedIndex; 

    ListBoxItem lbi = Results.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem; 

    CreateContextMenu(lbi); 
    Results.SelectedIndex = -1; 
} 
相關問題