[ Windows Phone ] WPtoolkit_長壓控制項跳出ContextMenu

想產生像桌電中的右鍵選單嗎?用WPtoolkit的ContextMenu就對了!

根據上一篇WPtoolkit

這裡也有個功能可以產生等同於在桌電的右鍵選單

稱作ContextMenu

 


 

如同上一篇的方法載入WPtoolkit

然後輸入

xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

 

接著輸入

<toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu>
                    <toolkit:MenuItem Header="selection 1"/>
                    <toolkit:MenuItem Header="selection 2"/>
                    <toolkit:MenuItem Header="selection 3"/>
                </toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>

 

這段程式碼擺放的位置會影響你的控制項是否有此功能。

 

例如放置在Button中,則只會對此Button有效果:

        <Button VerticalAlignment="Center"
             Content="ContextMenu">
            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu>
                    <toolkit:MenuItem Header="selection 1" Click="MenuItem_Click"/>
                    <toolkit:MenuItem Header="selection 2" Click="MenuItem_Click"/>
                    <toolkit:MenuItem Header="selection 3" Click="MenuItem_Click"/>
                </toolkit:ContextMenu>
            </toolkit:ContextMenuService.ContextMenu>
        </Button>

 

 

以下小小的Demo

 

xaml

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Button Margin="0,0"     
             VerticalAlignment="Center"
             HorizontalAlignment="Center"
             Content="ContextMenu"    
             FontSize="25">
            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu>
                    <toolkit:MenuItem Header="selection 1" Click="MenuItem_Click"/>
                    <toolkit:MenuItem Header="selection 2" Click="MenuItem_Click"/>
                    <toolkit:MenuItem Header="selection 3" Click="MenuItem_Click"/>
                </toolkit:ContextMenu>
            </toolkit:ContextMenuService.ContextMenu>
        </Button>
        <TextBlock x:Name="lastSelection" 
                   HorizontalAlignment="Center" 
                   Margin="0,135,0,0" 
                   TextWrapping="Wrap" 
                   VerticalAlignment="Top">
            shows the last selection here
        </TextBlock>
    </Grid>

xaml.cs

private void MenuItem_Click(object sender, RoutedEventArgs e)
{	
	lastSelection.Text = (string)((MenuItem)sender).Header;  
}

 

1                  2                 3

 


 

這只是片段功能,希望大家能應用在不同的地方上囉:)

 

 

 

 

Reference : http://myprogrammingdial.blogspot.tw/2011/12/introduction-of-using-windows-phone_666.html