Windows Phone 8.1中ListView中的MenuFlyout

时间:2014-09-21 11:22:01

标签: windows-phone-8.1 win-universal-app

我正在开发一个通用的Windows项目。

Windows Phone 8.1 UI包含列表视图。 Listviews的源代码是数据绑定,其datatemplate包含一个按钮。我想在按下按钮时显示一个MenuFlyout MenuFlyout(如wp8工具包中的ContextMenu)。

我的代码是:

    <DataTemplate x:Key="ListTemplate" x:Name="ListTemplate">
        <Button Style="{StaticResource ListButtonStyle}">
            <Button.Flyout> 
                <MenuFlyout>
                        <MenuFlyoutItem Text="delete"/>
                </MenuFlyout>
            </Button.Flyout>
        </Button>
    </DataTemplate>

    <ListView ItemsSource="{Binding List}" ItemTemplate="{StaticResource ListTemplate}">

但是,这会在单击按钮时打开MenuFlyout。如何将此行为更改为按住事件以打开MenuFlyout?

此外,MenuFlyout会放大打开它的按钮。如何禁用此缩放效果?

2 个答案:

答案 0 :(得分:0)

您可以从Windows Phone Toolkit修改ContextMenuService

有很好的教程。

http://www.visuallylocated.com/post/2014/05/29/Migrating-from-the-Windows-Phone-Toolkit-ContextMenu-to-the-new-Runtime-MenuFlyout.aspx

简而言之:

  • 替换ContextMenu
  • MenuFlyout
  • 添加事件处理程序OnElementHolding

    private void OnElementHolding(object sender, HoldingRoutedEventArgs args)
    {
        // this event is fired multiple times. We do not want to show the menu twice
        if (args.HoldingState != HoldingState.Started) return;
    
        FrameworkElement element = sender as FrameworkElement;
        if (element == null) return;
    
        // If the menu was attached properly, we just need to call this handy method
        FlyoutBase.ShowAttachedFlyout(element);
     }
    
  • 修改OnMenuFlyoutChanged

    private static void OnMenuFlyoutChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement element = o as FrameworkElement;
        if (null != element)
        {
            // just in case we were here before and there is no new menu
            element.Holding -= OnElementHolding;
    
            MenuFlyout oldMenuFlyout = e.OldValue as MenuFlyout;
            if (null != oldMenuFlyout)
            {
                element.SetValue(FlyoutBase.AttachedFlyoutProperty, null);
            }
            MenuFlyout newMenuFlyout = e.NewValue as MenuFlyout;
            if (null != newMenuFlyout)
            {
                element.SetValue(FlyoutBase.AttachedFlyoutProperty, newMenuFlyout);
                element.Holding += OnElementHolding;
            }
        }
    }
    

答案 1 :(得分:0)

将按钮放在网格中并将menuflyout连接到网格而不是按钮。

像这样:

<DataTemplate x:Key="ListTemplate" x:Name="ListTemplate">
 <Grid Holding="Grid_Holding">
    <Button Style="{StaticResource ListButtonStyle}">
    </Button>
    <FlyoutBase.AttachedFlyout>
      <MenuFlyout>
        <MenuFlyoutItem Text="delete"/>
      </MenuFlyout>
    </FlyoutBase.AttachedFlyout>
 </Grid>
</DataTemplate>

在代码背后:

    private void Grid_Holding(object sender, Windows.UI.Xaml.Input.HoldingRoutedEventArgs e)
    {
        if (e.HoldingState == Windows.UI.Input.HoldingState.Started)
        {
            FrameworkElement frameworkElement = sender as FrameworkElement;
            FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(frameworkElement);
            flyoutBase.ShowAt(frameworkElement);
        }
    }