c# - 如何在 Windows Phone 8.1 的 ListView 中正确设置 'ContextMenu'?

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

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

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

最佳答案

点击事件触发后,您可以获得 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)
}

关于c# - 如何在 Windows Phone 8.1 的 ListView 中正确设置 'ContextMenu'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26087806/

相关文章:

javascript - 整数和小数小时 Double 值转换为 12 小时制时钟格式时间

C# LINQ,删除所有相等的元素

c# - 相当于使用 Outlook DASL 筛选器选择位置

windows - 在 Windows 终端预览中运行 Clink

c# - 如何将 xaml 中的值绑定(bind)到验证规则?

c# - 尝试实现 IEnumerable 时为数组实现包装类时出现问题

c++ - 调用 SHGetSpecialFolderLocation() 函数时,系统帐户和管理员帐户有什么不同?

java - 如果我只知道SID,如何更改Windows中的文件ACL?

wpf - 如何在 WPF UserControl 中创建样式?

c# - 为什么我的命令不能启用按钮?