2016-02-26 61 views
0

我有最終將發佈的平板電腦的Windows 8.1集線器應用程序。我想將MenuFlyout添加到GridView。所以如果我右鍵單擊或按住,則會顯示彈出窗口。我已經瀏覽了幾個視頻,教程等,但沒有一個與GridView有關。僅適用於WindowsPhone上的ListView。所以他們都沒有表明我應該如何處理。MenuFlyout在Windows 8.1中的GridView中的項目HubApp

可能嗎?我應該如何處理它?

HubSection

這是怎麼了我的HubsectionGridView模樣。

<HubSection x:Name="CalcButtons" x:Uid="calculator" Header="{Binding Res.calc2Header, Source={StaticResource SharedStrings}}"> 
    <DataTemplate> 
     <GridView 
      ItemsSource="{Binding}" 
      AutomationProperties.AutomationId="ItemGridView" 
      AutomationProperties.Name="Items In Group" 
      ItemTemplate="{StaticResource StandardCalcButton}" 
      SelectionMode="None" 
      IsItemClickEnabled="True" Margin="0,0,0,0" ItemClick="GridView_ItemClick"> 
      <GridView.ItemsPanel> 
       <ItemsPanelTemplate> 
        <ItemsWrapGrid/> 
       </ItemsPanelTemplate> 
      </GridView.ItemsPanel> 
     </GridView> 
    </DataTemplate> 
</HubSection> 

StandardCalcButton

我在App.xaml中創建MenuFlayout的資源,並將其連接到GridViewItem的DataTemplate。所以它看起來像這樣。

<MenuFlyout x:Key="CalcFlyout"> 
    <MenuFlyoutItem Text="Test1"/> 
    <MenuFlyoutItem Text="Test2"/> 
</MenuFlyout> 

<DataTemplate x:Key="StandardCalcButton" > 
    <Grid Height="180" Width="180" Margin="5,5,5,5" FlyoutBase.AttachedFlyout="{StaticResource CalcFlyout}" > 
     <Border Height="180" Background="{StaticResource AppYellow}"> 
      <StackPanel Grid.Row="1" Margin="0,0,0,0"> 
       <Image Source="{Binding ImagePath}" AutomationProperties.Name="{Binding Title}" Height="130" Width="180"/> 
       <TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap" TextLineBounds="Tight" TextAlignment="Center"/> 
       <TextBlock Text="{Binding Subtitle}" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" TextLineBounds="Tight" LineStackingStrategy="BlockLineHeight" TextTrimming="CharacterEllipsis" TextAlignment="Center" /> 
      </StackPanel> 
     </Border> 
    </Grid> 
</DataTemplate> 

這是我想使彈出按鈕在GridView_ItemClicked

private void GridView_ItemClick(object sender, ItemClickEventArgs e){ 
    FrameworkElement senderElement = e.ClickedItem as FrameworkElement; 
    if (senderElement == null) 
    { 
     Debug.WriteLine("null"); 
     return; 
    } else 
    { 
     FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(senderElement); 
     flyoutBase.ShowAt(senderElement); 
    } 
} 

'ClickedItem「打開」爲對象,我提供的'GridView.DataContext'所以沒有辦法,它可以轉換爲FrameworkElement。

現在我卡住了。我會感謝任何指導。

回答

0

你可以嘗試以下方法:

private void GridView_ItemClick(object sender, ItemClickEventArgs e) 
    { 

     var gridViewItemsControl = sender as ItemsControl; 
     var clickedItemContainer = gridViewItemsControl.ContainerFromItem(e.ClickedItem); 

     Debug.WriteLine(clickedItemContainer); 

     var clickedGridViewItem = clickedItemContainer as GridViewItem; 
     var clickedItemGrid = clickedGridViewItem.ContentTemplateRoot as Grid; 

     FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(clickedItemGrid); 
     flyoutBase.ShowAt(clickedItemGrid); 

    } 

希望我幫助! :)

+0

你是我的英雄:)謝謝 –

相關問題