【问题标题】:How to correctly set up a 'ContextMenu' in a ListView for Windows Phone 8.1?如何在 Windows Phone 8.1 的 ListView 中正确设置“ContextMenu”?
【发布时间】:2014-11-23 03:10:18
【问题描述】:

MenuFlyout 有问题。我正在尝试获得一个运行良好的上下文菜单,以便为用户提供“删除”和“编辑”选项。但是,如果用户单击这些选项之一,似乎没有关于如何获取列表视图或所选项目的解决方案。也许我只是对某些事情感到困惑,但是我搜索了一整天,即使人们遇到了类似的问题,但没有一个解决方案对我有用。

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

【问题讨论】:

    标签: c# windows xaml windows-phone-8.1 flyout


    【解决方案1】:

    一旦引发点击事件,您就可以获得 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)
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      相关资源
      最近更新 更多