2014-09-28 80 views
7

我對MenuFlyout有問題。我試圖得到一個上下文菜單,這很好,給用戶選項「刪除」和「編輯」。但是,如果用戶點擊其中一個選項,似乎沒有解決方法來獲取列表視圖或所選項目。也許我只是對某些事情感到困惑,但我搜索了整整一天,即使人們遇到了類似的問題,但沒有任何解決方案爲我工作。如何在Windows Phone 8.1的ListView中正確設置'ContextMenu'?

XAML

<Pivot x:Name="MyPivot" Title="MyTitle" ItemsSource="{Binding}"> 
     <Pivot.HeaderTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Title}"/> 
      </DataTemplate> 
     </Pivot.HeaderTemplate> 

     <Pivot.ItemTemplate> 
      <DataTemplate> 
       <ScrollViewer> 
        <ListView x:Name="MyListView" ItemsSource="{Binding Items}"> 
         <ListView.ItemContainerStyle> 
          <Style TargetType="ListViewItem"> 
           <Setter Property="HorizontalAlignment" Value="Stretch"/> 
           <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
           <Setter Property="Margin" Value="0,0,0,10"/> 
          </Style> 
         </ListView.ItemContainerStyle> 

         <ListView.ItemTemplate> 
          <DataTemplate> 
           <Grid Holding="Grid_Holding"> 
            <Grid.ColumnDefinitions> 
             <ColumnDefinition Width="Auto"/> 
             <ColumnDefinition Width="*"/> 
             <ColumnDefinition Width="Auto"/> 
            </Grid.ColumnDefinitions> 

            <FlyoutBase.AttachedFlyout> 
             <MenuFlyout> 
              <MenuFlyoutItem x:Name="EditButton" 
                  Text="Edit" 
                  Click="EditButton_Click"/> 
              <MenuFlyoutItem x:Name="DeleteButton" 
                  Text="Delete" 
                  Click="DeleteButton_Click"/> 
             </MenuFlyout> 
            </FlyoutBase.AttachedFlyout> 

            // Content (TextBlocks...) 

           </Grid> 
          </DataTemplate> 
         </ListView.ItemTemplate> 
        </ListView> 
       </ScrollViewer> 
      </DataTemplate> 
     </Pivot.ItemTemplate> 
    </Pivot> 

C#

private void Grid_Holding(object sender, HoldingRoutedEventArgs e) 
    { 
     FrameworkElement senderElement = sender as FrameworkElement; 
     FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(senderElement); 
     flyoutBase.ShowAt(senderElement); 
    } 

回答

8

一旦你點擊事件引發的,你可以得到FrameworkElement的的DataContext的。

private void EditButton_Click(object sender, RoutedEventArgs e) 
{ 
    var datacontext = (e.OriginalSource as FrameworkElement).DataContext; 

    //this datacontext is probably some object of some type T (whatever is in your Items collections you haven't specified in your question) 
} 
+0

非常感謝我的工作方式。 – Cort3vl 2014-09-28 19:59:27

相關問題