在 Windows Phone 中有一个称为ContextMenu的功能,它允许为控件创建弹出菜单。
但是,如果菜单项列表很大,则其中一些不适合屏幕。下面的简单示例将说明这一点:
在xml中:
我们将使用 Toolkit,因此添加
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
然后例如:
<Button Content="Button 1" Height="72" HorizontalAlignment="Left" Margin="160,400,0,0" Name="button1" VerticalAlignment="Top" Width="160">
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Action A" Name="miActionA1" BorderBrush="Black" BorderThickness="1" Click="nullAction" />
<toolkit:MenuItem Header="Action B" Name="miActionB1" BorderBrush="Black" BorderThickness="1" Click="nullAction" />
<toolkit:MenuItem Header="Action C" Name="miActionC1" BorderBrush="Black" BorderThickness="1" Click="nullAction" />
<toolkit:MenuItem Header="Action D" Name="miActionD1" BorderBrush="Black" BorderThickness="1" Click="nullAction" />
<toolkit:MenuItem Header="Action E" Name="miActionE1" BorderBrush="Black" BorderThickness="1" Click="nullAction" />
<toolkit:MenuItem Header="Action F" Name="miActionF1" BorderBrush="Black" BorderThickness="1" Click="nullAction" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</Button>
在 nullAction 我们什么都不做
private void nullAction(object sender, RoutedEventArgs e)
{
}
DesignHeight 值是默认 WP7 页面的标准值DesignHeight="768"
如您所见,当您长按“按钮 1”控件时,会显示菜单,但不会完全显示。有些项目是不可见的。就我而言,最好的决定是让这个菜单可以滚动。这可以使用 ScrollViewer来完成。
因此,我们使用 Scrollviewer 和 StackPanel 覆盖菜单项。
<Button Content="Button 1" Height="72" HorizontalAlignment="Left" Margin="160,400,0,0" Name="button1" VerticalAlignment="Top" Width="160">
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<ScrollViewer>
<StackPanel>
<toolkit:MenuItem Header="Action A" Name="miActionA1" BorderBrush="Black" BorderThickness="1" Click="nullAction" />
<toolkit:MenuItem Header="Action B" Name="miActionB1" BorderBrush="Black" BorderThickness="1" Click="nullAction" />
<toolkit:MenuItem Header="Action C" Name="miActionC1" BorderBrush="Black" BorderThickness="1" Click="nullAction" />
<toolkit:MenuItem Header="Action D" Name="miActionD1" BorderBrush="Black" BorderThickness="1" Click="nullAction" />
<toolkit:MenuItem Header="Action E" Name="miActionE1" BorderBrush="Black" BorderThickness="1" Click="nullAction" />
<toolkit:MenuItem Header="Action F" Name="miActionF1" BorderBrush="Black" BorderThickness="1" Click="nullAction" />
</StackPanel>
</ScrollViewer>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</Button>
成功?不,因为您可以滚动此列表,但不能向下选择项目。
我的问题是如何解决它?
使用多点触控?(我对此了解不多)
也许,我需要向 Scrollviewer 添加一些属性?
还有其他想法吗?
[更新]此外,如果添加了滚动查看器,则发现项目的点击/单击处理程序会更改行为。通常,当您点击该项目时,上下文菜单会关闭并执行处理程序方法。使用scrollviewer,上下文菜单不会关闭,需要再点击一次才能手动关闭。为什么会这样?