Tom*_*rez 36 c# xaml longlistselector windows-phone-8
我在Windows Phone 8上使用LongListSelector控件,无法找出处理项目点击的最佳方法.我发现的几个例子都依赖于SelectionChanged事件.但是,此解决方案有问题,因为如果我点击打开新页面的项目,回击,然后再次点击相同的项目,它将无法工作,因为此项目已被选中,因此不会触发SelectionChanged.
我尝试注册到点击事件并使用当前所选项目作为点击事件,但有时候当前所选项目不是我期望的项目.
我可以将我的ItemTemplate包装在一个按钮中并处理每个项目的点击,但我需要重新设置按钮以使其看起来像一个简单的列表项.
最后,我不明白为什么实现这样一个基本的东西是如此复杂.我错过了一种简单而标准的方式吗?
我的第二个愿望是在点击项目时对项目产生影响.有没有标准的方法呢?
Ste*_*vie 41
您可以在每个事件结束时使用nullLongListSelector .即SelectedItemSelectionChanged
<phone:LongListSelector x:Name="LLS" SelectionChanged="LLS_SelectionChanged">
Run Code Online (Sandbox Code Playgroud)
和事件处理程序:
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;
}
Run Code Online (Sandbox Code Playgroud)
你将两次触发SelectionChanged事件,但第二次没有任何事情发生,你应该得到你正在寻找的行为 - (即设置SelectedItem为null将触发一个新SelectionChanged事件,但第二个事件被捕获在if中-声明)
至于问题的第二部分,您可能最好发布一个新问题.
我用Tap事件处理完成了它.
我不想使用Selected属性,但是以这种方式获取tapped项目(我没有发现任何错误):
MyListItemClass item = ((FrameworkElement)e.OriginalSource).DataContext
as MyListItemClass;
Run Code Online (Sandbox Code Playgroud)
此外,您可以通过从e.OriginalSource导航到VisualTree来简化原始项目ContentPresenter.那样:
ContentPresenter itemPresenter = SomeHelperClass
.FindParent<ContentPresenter>(e.OriginalSource,"");
Run Code Online (Sandbox Code Playgroud)
FindParent类似于在这个问题中找到孩子:如何通过名称或类型找到WPF控件?
ContentPresenter是您想要手动更改项目模板所需的对象(例如,设置"选定"状态).