2016-12-18 126 views
1

我只想在gridViewItem上的righttapped事件後彈出菜單。右擊事件後的菜單彈出

我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> 

我升C代碼:

 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"); } 
    } 

我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; 
    } 
} 

然後,出放印有「無f值」

請幫忙,謝謝!

回答

1

發件人是GridViewItem的項目,FlyoutBase在GridViewItem中。

您的FlyoutBase f = FlyoutBase.GetAttachedFlyout(s);一直爲空,因爲GridViewItem的項目沒有Flyout。

您可以使用定義代碼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)); 
+0

它的工作原理。非常感謝你 –

+0

這是我項目的2個部分中的1個。 非常感謝您的有益建議。 如果你有時間,請檢查我的第2部分(是的,它也有錯誤....) –

+0

鏈接?我不知道你說什麼。 – lindexi