简体   繁体   English

在LongListSelector中获取选定的索引

[英]Get the selected index in a LongListSelector

I'm using a LongListSelector for a WP8 app. 我正在为WP8应用程序使用LongListSelector。 I searched on several websites but did not found if there was a way to know the index of the item the user taped in the List. 我在几个网站上进行了搜索,但没有找到是否有一种方法可以知道用户在列表中记录的项目的索引。 If anyone has an idea, it would be nice. 如果有人有想法,那就太好了。 Thanks 谢谢

` `

        <phone:LongListSelector x:Name="ListeNotes" Height="535" Width="426" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="36" Margin="54,0,0,0">

            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>

                    <TextBlock Text="{Binding Titre}"/>

                </DataTemplate>

            </phone:LongListSelector.ItemTemplate>

        </phone:LongListSelector>

` `

I'd create in code-beind or ViewModel (depending on what are you using), public variable 我将在代码隐藏或ViewModel(取决于您使用的是什么)中创建公共变量

Public Int32 itemSelectedIndex {get;set;} //This is a public variable, therefore add it inside your class

and bind it to SelectedIndex as following: 并将其绑定到SelectedIndex ,如下所示:

<phone:LongListSelector x:Name="ListeNotes" Height="535" Width="426" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="36" Margin="54,0,0,0" SelectedIndex ="{Binding itemSelectedIndex, UpdateSourceTrigger = PropertyChanged}">

don't forget to add datacontext reference in code behind 不要忘记在后面的代码中添加datacontext引用

Public void MainWindow()
{
InitializeComponents();
this.DataContext = this;//this makes sure that you can bind public varibles to XAML
}

Afterwards you simply reference itemSelectedIndex anywhere in your code and it will return selected value (eg System.Windows.MessageBox.Show(itemSelectedIndex.ToString()); 之后,您只需在代码中的任何地方引用itemSelectedIndex,它将返回选定的值(例如System.Windows.MessageBox.Show(itemSelectedIndex.ToString());

In the handler: 在处理程序中:

SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles ListeNotes.SelectionChanged`

The "sender" is the ListBox . “发送者”是ListBox So all you have to do is cast "sender" to ListBox , and use its SelectedIndex property. 因此,您要做的就是将“发送者” ListBoxListBox ,并使用其SelectedIndex属性。

Dim listBox As ListBox = CType(sender, ListBox)
Dim tappedIndex = listBox.SelectedIndex

你可以通过

Dim num As Integer = (sender as LonglistSelector).Datasource.IndexOf((sender as LonglistSelector).SelectedItem)

找不到解决方案...实际上,我只是说用户不能拥有相同的Note对象两次,并且我在List(Of Note)中使用IndexOf方法来获取LongListSelector中SelectedItem的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM