在Android开发中,上下文菜单被大量使用到,而WP7中则更重视Application Bar的使用。在Sliver light toolkit的提供的ContextMenu,简单来说,当用户Tap并Hold住一个对象时候,Context菜单就会出现,例如在Application List中,按下/holde某个应用的图标,会提示Unpin或者Uninstall等。
在使用方面,应用DLL. C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Toolkit\Nov10\Bin\Microsoft.Phone.Controls.Toolkit.dll.在XAML页面中,添加Namesapce, xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
将ContextMenu的xAML内容嵌套在具体的Item申明内:
<ITEM....>
<
toolkit:ContextMenuService.ContextMenu
>
<
toolkit:ContextMenu
>
<
toolkit:MenuItem
Header
=
"Item1"
/>
<
toolkit:MenuItem
Header
=
"Item2"
/>
<
toolkit:MenuItem
Header
=
"Item3"
/>
<
toolkit:MenuItem
Header
=
"Item4"
/>
<
toolkit:MenuItem
Header
=
"Item5"
/>
</
toolkit:ContextMenu
>
</
toolkit:ContextMenuService.ContextMenu
></ITEM...>
<toolkit:ContextMenu x:Name="databoundMenu">
<toolkit:ContextMenu.ItemContainerStyle>
<Style TargetType="toolkit:MenuItem">
<Setter Property="Background" Value="YellowGreen" />
<Setter Property="Margin" Value="5" />
</Style>
</toolkit:ContextMenu.ItemContainerStyle>
</toolkit:ContextMenu>
通过Style 控制ContextMenu item的布局:
<
toolkit:ContextMenu.ItemContainerStyle
>
<
Style
TargetType
=
"toolkit:MenuItem"
>
<
Setter
Property
=
"Background"
Value
=
"YellowGreen"
/>
<
Setter
Property
=
"Margin"
Value
=
"5"
/>
</
Style
>
</
toolkit:ContextMenu.ItemContainerStyle
>
也可以动态绑定Item内容:
List<
string
> menuItems =
new
List<
string
>() {
"Menu item 1"
,
"Menu item 2"
,
"Menu item 3"
};
this
.databoundMenu.ItemsSource = menuItems;
原文参考以及Sample下载: http://www.windowsphonegeek.com/articles/WP7-ContextMenu-in-depth--Part1-key-concepts-and-API