1
一切都很简单:我有一个ListBoxSelector(应用程序用于wp7)。项目由图片和一些文本组成。项目样式存储在StyleTemplate内部。图像可以挖掘,其事件就像EventToCommand可能带来的Mvvm-light错误
<Style x:Key="ProductSearchListViewerItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid>
<Border BorderThickness="0,0,0,0.5">
<Grid>
<Border Grid.Column="0" Background="Transparent">
<Grid> !!! Image is here
</Grid>
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="Tap">
<command:EventToCommand
Command="{Binding Source={StaticResource ViewModelLocator}, Path=NavigationViewModel.OnProductSearchListViewerTapImageCommand, Mode=OneWay}"
CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext}"/>
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>
</Border>
...and so on...
事件本身:
private void OnProductSearchListViewerTapImage(ProductItem item)
{
if (item == null) return;
MessengerInstance.Send<ProductItem, ShoppingCartViewModel>(item);
}
到目前为止好。运行时,一切正常...... 95%的情况。当我按下某件物品的图像时,我收到了包含适当物品的物品列表,事件已上升,命令已发送,正确的物品已添加到购物车中。
但是。在某些情况下(问题开始,当点击第24-25项时),将错误的项目添加到购物车(列表呈现正确)。
- 首先我做了 - 检查命令:它包含错误的项目。比如说,我正在攻击第24项,但是在命令中,“项目”是第一个从列表中选取的。
- 其次我做了什么 - 我抓住了XamlSpy并检查了什么是该项目的DataContext。它保存了第24项,因为它应该是(因为项目的图像和描述已正确呈现)。
这意味着问题出在Event和Command之间。所以,EventToCommand内部有些问题。
任何人都有相同的经验或任何想法?