右击事件后的菜单弹出窗口

时间:2016-12-18 05:22:39

标签: c# uwp

我只是想在gridViewItem上发布一个正确的事件后制作一个菜单弹出窗口。

我的xaml代码:

<Page
x:Class="HNT_listView2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:HNT_listView2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:data="using:HNT_listView2.Models"
mc:Ignorable="d">

<Grid Background= "Salmon" Margin="0,0,10,0"  >

    <GridView ItemsSource="{x:Bind MyContactList}" 
              ItemClick="GridViewItem_Click" Name="NameOf_ItemClick"
              IsItemClickEnabled="True"
              RightTapped="GridViewItem_RightTapped"
              IsRightTapEnabled="True">


        <GridView.ItemTemplate>
            <DataTemplate x:DataType="data:Contact">
                <StackPanel >
                    <FlyoutBase.AttachedFlyout>
                        <MenuFlyout Placement="Top">
                            <MenuFlyoutItem Text="Call"/>
                            <MenuFlyoutItem Text="Send a message"/>
                            <MenuFlyoutItem Text="Delete"/>
                        </MenuFlyout>
                    </FlyoutBase.AttachedFlyout>

                    <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">


                    <Image Width="100" Height="120" Source="{x:Bind Photo}" HorizontalAlignment="Center" Stretch="UniformToFill"/>
                    <StackPanel Orientation="Vertical">
                        <TextBlock FontSize="30" Text="{x:Bind Name}" HorizontalAlignment="Center"/>
                        <TextBlock FontSize="30" Text="{x:Bind Phone}" HorizontalAlignment="Center"/>
                    </StackPanel>
                </StackPanel>
                </StackPanel>
            </DataTemplate>
        </GridView.ItemTemplate>

    </GridView>


</Grid>

My C sharp code:

        private void GridViewItem_RightTapped(object sender, RightTappedRoutedEventArgs e)
    {
        var s = (FrameworkElement)sender;

        if (s != null)
        {
            FlyoutBase f = FlyoutBase.GetAttachedFlyout(s);
            if (f != null)
            {
                f.ShowAt(s);
            }
            else {Debug.WriteLine("No f value");}                
        }
        else { Debug.WriteLine("No s value"); }
    }

My Contact.cs(用于绑定):

 public class Contact
{
    public string Name { get; set; }
    public string Photo { get; set; }
    public string Phone { get; set; }
}

public class ContactManager
{
    public static List<Contact> GetContacts()
    {
        var contact1 = new List<Contact>();
        contact1.Add(new Contact { Name = "Nguyen Van A", Phone = "0168111222", Photo = "Assets/1.jpg" });
        contact1.Add(new Contact { Name = "Tran Van B", Phone = " 0168333444", Photo = "Assets/2.jpg" });
        contact1.Add(new Contact { Name = "Le Van C", Phone = "0166555666", Photo = "Assets/3.jpg" });

        return contact1;
    }
}

然后输出输出&#34;没有f值&#34;

请帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

发件人是GridViewItem的项目,FlyoutBase在GridViewItem中。

您的FlyoutBase f = FlyoutBase.GetAttachedFlyout(s);总是为空,因为GridViewItem的项目没有弹出窗口。

您可以使用定义代码flyoutbase。

MenuFlyout myFlyout = new MenuFlyout();
MenuFlyoutItem callItem = new MenuFlyoutItem { Text = "Call" };
MenuFlyoutItem sendItem = new MenuFlyoutItem { Text = "Send a message" };
MenuFlyoutItem deleteItem = new MenuFlyoutItem { Text = "Delete" };

myFlyout.Items.Add(callItem );
myFlyout.Items.Add(sendItem );
myFlyout.Items.Add(deleteItem );

并在点击位置设置弹出按钮。

myFlyout.ShowAt(sender as UIElement, e.GetPosition(sender as UIElement));