WP 8.1 Universal APP part 3 ListView with ContextMenu(MenuFlyout)

WP 8.1 Universal APP part 3 ListView with ContextMenu(MenuFlyout)

現在在WP8.1上多了很多Flyout可以使用!如標題所說的要說明的是兩個FlyOut

在ListView上的Item沒有ContextMenu可以使用,但可以使用MenuFlyout來達到一樣的效果!

在Xaml上做一個ListView

 


 

在MainPage.xaml的Resource區塊加入Item 的 DataTemplate以及 Flyout的樣板

 <Grid HorizontalAlignment="Stretch" VerticalAlignment="Center">
  <TextBlock Text="{Binding Index}" FontFamily="{StaticResource PhoneFontFamilySemiLight}" FontSize="28" Margin="7,0"/>
 </Grid>
</DataTemplate>
 <MenuFlyoutItem Text="FlyoutItem1" Command="{StaticResource MFCmd}" CommandParameter="{Binding}"/>
 <MenuFlyoutItem Text="FlyoutItem2" Command="{StaticResource MFCmd}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Text}"/>
 <MenuFlyoutItem Text="FlyoutItem3" Command="{StaticResource MFCmd}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Text}"/>
</MenuFlyout>

在Code-Behide的地方加上

        {
            var flyout = (MenuFlyout)Resources["MenuFlyoutSource"];
            switch (e.HoldingState)
            {
                case Windows.UI.Input.HoldingState.Started:
                    if (e.OriginalSource is TextBlock)
                    {
                        var tb = e.OriginalSource as TextBlock;
                        flyout.ShowAt(tb);
                    }
                    else
                    {
                        foreach (var item in ((e.OriginalSource as Border).Child as Grid).Children)
                        {
                            if (item is ContentPresenter)
                            {
                                var cp = (item as ContentPresenter);
                                flyout.ShowAt(cp);
                                break;
                            }
                        }
                    }
                    break;
                case Windows.UI.Input.HoldingState.Completed:
                case Windows.UI.Input.HoldingState.Canceled:
                    break;
            }
        }

這樣就可以使用Menu在ListView的Item上了。