点击手势事件+长列表选择器+分组标题点击
本文关键字:标题 选择器 事件 列表 | 更新日期: 2024-09-23 09:54:18
我遇到了一个问题:我有一个LonglistSelector,如下所示:
<phone:LongListSelector Name="ListContacts"
ItemsSource="{Binding GroupedPeople}"
JumpListStyle="{StaticResource LongListSelectorJumpListStyle}"
GroupHeaderTemplate="{StaticResource LongListSelectorGroupHeaderTemmplate}"
ItemTemplate="{StaticResource LongListSelectorItemTemplate}"
HideEmptyGroups ="True" IsGroupingEnabled ="true" LayoutMode="List">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<mvvmlight:EventToCommand Command="{Binding ListContactsTapCommand, Mode=OneTime}" CommandParameter="{Binding ElementName=ListContacts, Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</phone:LongListSelector>
它与此链接中的PeopleHub非常相似:http://code.msdn.microsoft.com/wpapps/PeopleHub-Windows-Phone-80-88abe94d
我的问题是tap事件。当我第一次点击一封信时,一切都如预期的那样发生了。然后,我点击联系人查看更多信息,一切都很好。
当我再次点击字母时,问题就会发生,因为tap事件被触发了两次(我想):一次是因为SelectedItem不为null,另一次是为了显示字母的。
在我的ViewModel中,我有这个:
public RelayCommand<Contact> ListContactsTapCommand { get; private set; }
.....
this.ListContactsTapCommand = new RelayCommand<Contact>(contact => ShowContactInformation(contact), contact => contact != null);
private void ShowContactInformation(Contact c)
{
ServiceLocator.Current.GetInstance<ContactInfoViewModel>().ContactInfo = c;
_navigationService.NavigateTo(new Uri(ViewModelLocator.ContactInfoPage, UriKind.Relative));
}
我认为可能的解决方案是重置SelectedItem,或者是一种知道我在哪里点击的方法。
有人能帮忙吗?提前谢谢。
问候
以我不完全同意的方式解决了我的问题。。。它"打破"了mvvm模式一点
这就是我所做的
视图:
<mvvmlight:EventToCommand Command="{Binding ListContactsTapCommand, Mode=OneTime}" CommandParameter="{Binding ElementName=ListContacts}"/>
ViewModel:
this.ListContactsTapCommand = new RelayCommand<LongListSelector>(param => ShowContactInformation(param), param => param.SelectedItem != null);
private void ShowContactInformation(LongListSelector c)
{
ServiceLocator.Current.GetInstance<ContactInfoViewModel>().ContactInfo = c.SelectedItem as Contact;
_navigationService.NavigateTo(new Uri(ViewModelLocator.ContactInfoPage, UriKind.Relative));
c.SelectedItem = null;
}
我不同意这种方法的原因是ViewModel必须从视图中了解对象。。。这是不应该的,但由于LonglistSelector不允许绑定到SelectedItem,这就是我的解决方案。
如果你们中的任何人有任何其他观点。。。我很高兴知道:)
再次提前感谢。