c# - WP8 : LongListSelector with ContextMenu to delete an item

标签 c# windows-phone-8 contextmenu longlistselector

我以为我的问题很简单,但我还是没有找到解决办法。
我在每个项目中都有一个 LongListSelector 和一个 ContextMenu 。当我长按 LongListSelector 的项目时,ContextMenu 将弹出并带有删除选项。我想删除选定的 LongListSelector 项目。我的代码:

XAML:

<phone:PhoneApplicationPage
....
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
>

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid x:Name="ContentPanel" Grid.Row="0" Margin="12,0,12,0">

        <phone:LongListSelector 
            Name="TestList"
            >
            <phone:LongListSelector.ItemTemplate
                >
                <DataTemplate>
                    <TextBlock Text="{Binding}">
                    <toolkit:ContextMenuService.ContextMenu>
                        <toolkit:ContextMenu Name="ContextMenu" >
                            <toolkit:MenuItem 
                                Name="Delete"  
                                Header="Delete" 
                                Click="Delete_Click"/>
                        </toolkit:ContextMenu>
                    </toolkit:ContextMenuService.ContextMenu>
                    </TextBlock>
                </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>

    </Grid>

</Grid>

C#:

namespace TestContextMenu
{
    public partial class MainPage : PhoneApplicationPage
    {
        public List<string> Items = new List<string>
        {
            "Item1",
            "Item2",
            "Item3",
            "Item4",
            "Item5",
        };

        public MainPage()
        {
            InitializeComponent();
            TestList.ItemsSource = Items;
        }


        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            Items.RemoveAt(0);
            //var item = (sender as MenuItem).DataContext;
            //TestList.ItemsSource.Remove(item);
        }
    }
}

当我单击“删除”时,LongListSelector 中的项目无法直观地删除,尽管数据已被删除。

我读到this ,但该解决方案不适用于我的情况。 谁知道我的代码有什么问题请告诉我,谢谢!

最佳答案

尝试替换List<string>ObservableCollection<string> 。因为 ObservableCollection 旨在对集合中的更改使用react。

关于c# - WP8 : LongListSelector with ContextMenu to delete an item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21432294/

相关文章:

c# - 如何避免使用 List<List<T>>?

c# - 为什么Workflow卸载后不唤醒?

java - 从 Windows 上下文菜单运行 Java 应用程序

c++ - 如何在 Internet Explorer 中显示我自己的上下文菜单

c# - 使用 UPnP.FindByType 查找 Belkin WeMo 设备

c# - C# 中的 Miller Rabin 素数测试

windows-phone-8 - 从 Windows Phone 8 设备中检索故障转储

html - 如何使用 HTML 在 Windows Phone 8 的 Azure 移动服务上对用户进行身份验证?

c# - "Rate It"Windows Phone 市场链接

python - 如何将 "New"Python 脚本选项添加到上下文菜单?