从“保留"事件中的"Windows Phone"列表框中获取选定的项目 [英] Get Selected Item from Windows Phone listbox in Hold event

查看:64
本文介绍了从“保留"事件中的"Windows Phone"列表框中获取选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Windows Phone 8应用程序中有一个非常基本的列表框.它不是数据绑定到任何东西,它只是一个空的列表框.

I have a pretty basic Listbox in my Windows phone 8 app. It's not data bound to anything, it's just an empty listbox.

<ListBox Margin="0,10" Name="lstStops" SelectionChanged="favouriteSelection" 
 Hold="favouriteSelectionHold" FontSize="28">
    <ListBox.Items>

    </ListBox.Items>
</ListBox>

我希望能够通过按住该项目,为用户提供从该列表中删除项目的选项.我添加了一个onHold事件,当用户按下列表中的某个项目并按住时会触发该事件,但是我不知道如何找出他们在列表框中选择的实际条目.

I want to be able to give the user the option to delete items from this list by pressing and holding on the item. I have added an onHold event which is firing when the user presses on an item in the list and holds but I don't know how to find out which actual entry in the listbox they have selected.

如何找到他们按住的项目?

How can I find out which item they have pressed and held on?

这是列表框的填充方式:

This is how the Listbox is populated:

foreach (KeyValuePair<string, Object> entry in IsolatedStorageSettings.ApplicationSettings)
{
    lstStops.Items.Add(entry.Key as String + " - " + entry.Value as String);
}

推荐答案

已经被问了很多遍了.有多种方法可以实现此目的.在此处了解更多信息.

It's been asked many times. There are different ways to achieve this. Read more here.

一种方法包括获取发件人的数据上下文并将其转换为您的Item类型.

One way includes getting the datacontext of the sender and casting it to your Item type.

由于它是一堆字符串,因此提取字符串的方法可以是:

Since it's a bunch of strings, the way to extract the string can be:

private void favouriteSelectionHold(object sender, System.Windows.Input.GestureEventArgs e)
{
    string n = (e.OriginalSource as TextBlock).Text;
}

这仅在您的DataTemplate定义不同的情况下有效.

This works only if your DataTemplate is not defined differently.

这篇关于从“保留"事件中的"Windows Phone"列表框中获取选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆