LongListSelector: Item tap?

随声附和 提交于 2019-11-27 12:02:49

You could null your LongListSelector's SelectedItem at the end of each SelectionChanged event. I.e.

<phone:LongListSelector x:Name="LLS" SelectionChanged="LLS_SelectionChanged">

And the event handler:

private void LLS_SelectionChanged(object sender, SelectionChangedEventArgs e) {

  // If selected item is null, do nothing
  if (LLS.SelectedItem == null)
    return;

  // Navigate to the next page
  NavigationService.Navigate(new Uri("/nextpage.xaml", UriKind.Relative));

  // Reset selected item to null
  LLS.SelectedItem = null;
}

You'll fire the SelectionChanged event twice, but nothing's going to happen the second time round and you should get the behaviour that you're looking for - (i.e Setting SelectedItem to null will trigger a new SelectionChanged event, but this second event gets caught in the if-statement)

As for the second part of your question, you might be better posting a new question.

gleb.kudr

I done it with the Tap event handling.

I prefer not to use Selected property, but get tapped item this way (and I haven't noticed any bugs):

MyListItemClass item = ((FrameworkElement)e.OriginalSource).DataContext 
                                                             as MyListItemClass;

Also, you could get the original item ContentPresenter simple by navigating up through VisualTree from e.OriginalSource. That way:

ContentPresenter itemPresenter = SomeHelperClass
                              .FindParent<ContentPresenter>(e.OriginalSource,"");

Where FindParent is similar to find child in this question: How can I find WPF controls by name or type?

ContentPresenter is that object what you need to manually change the item template if you want to (to set "selected" state for example).

 private void Item_tap(object sender, RoutedEventArgs e)
        {
            var element = (FrameworkElement)sender;
            DataSource data = (DataSource)element.DataContext;


        }

My second wish is to get an effect on the item when it is tapped. Is there any standard way to do it?

For this the only thing you need to do add this to your control (or stackpanel where you want to have this effect):

<StackPanel toolkit:TiltEffect.IsTiltEnabled="True">
AVIK DUTTA

first add this to *.xaml page inside the

LongListSelectorSelectionChanged="listBox_SelectionChanged"

so that it looks like this :

<toolkit:LongListSelector x:Name="listBox" SelectionChanged="listBox_SelectionChanged">

then in the *.xaml.cs file in the event handler

private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // Write your logic on what you want to do with the selected item
}

In addition to halil´s answer:

First of all you need to install the Windows Phone Toolkit (WPtoolkit) by NuGet. Than add the namespace declaration on the PhoneApplicationPage.

xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

After this you can add toolkit:TiltEffect.IsTiltEnabled="True" to the control definition.

It is nice documneted by NOKIA: http://developer.nokia.com/community/wiki/Tilt_Effect_for_Windows_Phone

Oliver

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