2
我認爲我的問題會非常簡單,但我仍然沒有找到解決方案。
我在每個項目中都有一個LongListSelector
和一個ContextMenu
。當我長按LongListSelector
的項目時,ContextMenu
將彈出一個刪除選項。我想刪除所選的LongListSelector
項目。我的代碼:WP8:使用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,但解決方案不適合我的情況。 任何人都知道我的代碼有什麼問題請告訴我,謝謝!
謝謝@ crea7or您的快速回復。它工作正常! – Vigor