How to handle a selected item of LongListMultiSelector?

二次信任 提交于 2019-12-25 05:03:47

问题


I want to be able:

  • to open a mail when the user taps on one item.
  • and delete multiple emails when the user selects multiple emails

So I choosed LongListMultiSelector.

In built in LongListSelector, I handle the SelectionChanged event like this:

private void mails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var selectedItem = mailsLongListSelector.SelectedItem as Mail;

    if (selectedItem == null)
        return;
    ...
    mailsLongListSelector.SelectedItem = null;
}

I want exactly like that functionality in wptoolkit's LongListMultiSelector. like when you select an email to open and read it.

LongListMultiSelector's SelectionChanged occurs when you tap left side of an item and checkboxes appear. this is not what I want.

The Question is: How can I perform something when the user taps on one item of LongListMultiSelector? thanks.


回答1:


You could try this. If this is your LongListSelector

<tkit:LongListMultiSelector Name="longlist" SelectionChanged="longlist_SelectionChanged">
    <tkit:LongListMultiSelector.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Title}" FontSize="32" Tap="TextBlock_Tap"/>
        </DataTemplate>
    </tkit:LongListMultiSelector.ItemTemplate>
</tkit:LongListMultiSelector>

and it has an itemtemplate, you can detect a tap on item.

private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    var itemTapped = (sender as FrameworkElement).DataContext as Book;
}

and still have a selection changed

private void longlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

}



回答2:


Once you are using LongListMultiSelector, the SelectionChanged event is fired when item is added or removed. If you want to perform the action regardless item is added/removed, I've managed to do it like this (for a simple string):

private void longlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string selectedItem = String.Empty;
    if (e.AddedItems.Count > 0) selectedItem = e.AddedItems[0] as string;
    else selectedItem = e.RemovedItems[0] as string;
    MessageBox.Show(selectedItem); // do your work
}

It should run while items are selected separately by tapping, but this method will have problems when more items are added/removed at the same time - if you need it, then you should handle this also.



来源:https://stackoverflow.com/questions/20478744/how-to-handle-a-selected-item-of-longlistmultiselector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!