我在ListBox下的我的xaml页面中使用ContextMenu来删除ListBox项目。我面临的问题是,当我长按ListBoxItem时,会出现删除选项并删除该项目,但是当我下次尝试时其他项目没有删除。任何人都可以告诉我如何点击ContextMenu.Menuitem我可以检索列表框项目详情。这是我的代码。
private void deleteitem_Click(object sender, RoutedEventArgs e)
{
NavigationContext.QueryString.TryGetValue("username", out username);
NavigationContext.QueryString.TryGetValue("password", out password);
ListBoxItem selecteditem = this.sniplist.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;
Item item = selecteditem.DataContext as Item;
HttpWebRequest deleterequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response=deleterequest.BeginGetResponse(new AsyncCallback(deleteitem),deleterequest) as HttpWebResponse;
//MessageBox.Show("Your item has been deleted");
}
deleteitem方法
private void deleteitem(IAsyncResult ar)
{
//throw new NotImplementedException();
HttpWebRequest request=(HttpWebRequest)ar.AsyncState;
HttpWebResponse response=(HttpWebResponse)request.EndGetResponse(ar);
if(response.StatusCode==HttpStatusCode.OK)
{
using(Stream respstream=response.GetResponseStream())
{
StreamReader reader = new StreamReader(respstream,
System.Text.Encoding.UTF8);
string parsestring = reader.ReadToEnd();
Debug.WriteLine("Response data:" + parsestring);
}
}
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
items.Remove(item1);
this.sniplist.ItemsSource = items;
MessageBox.Show("You item has been deleted");
if (sniplist.Items.Count == 0)
{
txt1.Text = "Please click + to add more items into your account";
}
});
}
}
}
请帮忙,我两天后就被困在这里了。
答案 0 :(得分:0)
我使用以下
解决了这个问题var item = ((MenuItem)sender).DataContext as ItemClass
ItemClass是包含不同项目的绑定详细信息的类