2014-09-28 9 views
7

MenuFlyout ile ilgili bir Sorunum var. Kullanıcıya 'sil' ve 'düzenle' seçeneklerini vermek için iyi çalışan bir içerik menüsü almaya çalışıyorum. Ancak kullanıcı bu seçeneklerden birine tıklarsa, liste görünümünü veya seçilen öğeyi nasıl alacağınız konusunda bir çözüm yok gibi görünüyor. Belki sadece bir şey hakkında kafam karıştı, ama bütün gün aradım ve insanların benzer problemleri olsa da, çözümlerin hiçbiri benim için işe yaramadı.Windows Phone 8.1 için bir ListView'de 'ContextMenu' nasıl ayarlanmalı?

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

cevap

8

tıklama etkinliği kaldırdı sonra, FrameworkElement ait DataContext'i alabilirsiniz.

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

Bu, istediğim gibi çalıştığından teşekkürler. – Cort3vl