我以为我的问题很简单,但我还是没有找到解决办法。
我在每个项目中都有一个 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/