【问题标题】:Get the selected index in a LongListSelector获取 LongListSelector 中的选定索引
【发布时间】:2013-12-22 16:24:32
【问题描述】:

我正在为 WP8 应用程序使用 LongListSelector。我在几个网站上进行了搜索,但没有找到是否有办法知道用户在列表中录制的项目的索引。 如果有人有想法,那就太好了。 谢谢

`

        <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>

`

【问题讨论】:

  • 是的,但在什么情况下?发布您的代码。
  • 没看懂问题...只是当用户选择一个项目时,它会打开第二个页面来编辑该项目。这是一个笔记应用程序。
  • 哪个代码?我在 VB 中没有任何东西,我不知道该怎么做......我只知道我必须在 SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles ListeNotes.SelectionChanged 事件上捕获它

标签: vb.net xaml windows-phone-8 longlistselector


【解决方案1】:

我会在代码隐藏或 ViewModel 中创建(取决于你使用的是什么),公共变量

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

并将其绑定到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}">

不要忘记在后面的代码中添加数据上下文引用

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

之后,您只需在代码中的任何位置引用 itemSelectedIndex,它就会返回选定的值(例如 System.Windows.MessageBox.Show(itemSelectedIndex.ToString());

【讨论】:

  • 我将第一个代码放在我的 MainPage 中,没有工作(我认为它是 C# 并且我发现的转换工具没有工作......)。第二个问题:我应该把它放在我的主要课程中吗?第二个代码,我把它放在 XAML 代码中,但在哪里?您可以在主题中看到我的 LLS 代码。
  • 我刚刚编辑了我的答案。希望你这次能成功! :)
  • 好的,这是 C#,但我设法将它放入 VB。所以现在的问题是The member SelectedIndex is not recognized or not accessible.The property SelectedIndex is not findable in the type LongListSelector.
  • 你可以在这里找到我的 MainPage 类:pastebin.com/a8UBMj3P 我的 LLS XAML 代码是
  • &lt;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}"&gt; &lt;phone:LongListSelector.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Titre}"/&gt; &lt;/DataTemplate&gt; &lt;/phone:LongListSelector.ItemTemplate&gt; &lt;/phone:LongListSelector&gt;
【解决方案2】:

在处理程序中:

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

“发件人”是ListBox。所以你所要做的就是将“发件人”转换为ListBox,并使用它的SelectedIndex 属性。

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

【讨论】:

  • 感谢您的回答。不幸的是,它不会像我想的那样工作:(我正在使用 LongListSelector,而不是 ListBox。它只是向我显示此错误消息:Unable to cast object of type 'Microsoft.Phone.Controls.LongListSelector' to type 'System.Windows.Controls.ListBox'.
  • 据我(和 Visual Studio)所知,LongListSelector 没有选定的索引属性。
【解决方案3】:

你可以通过

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

【讨论】:

  • 我必须对其进行编辑以使其“工作”并删除 as LongListSelector 两次。但是后来我收到了这个错误:没有定义库'Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSetComplex'。并且未定义库“Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet”。
  • 这个库需要哪些内容
【解决方案4】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-24
    • 2015-11-26
    • 2013-06-09
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多