【问题标题】:Possible Mvvm-light bug with EventToCommandEventToCommand 可能的 Mvvm-light 错误
【发布时间】:2013-05-17 17:33:30
【问题描述】:

一切开始都很简单:我有一个 ListBoxSelector(应用程序适用于 wp7)。项目由图片和一些文字组成。项目样式存储在 StyleTemplate 中。图片可以点击,它的事件是这样的

<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 个项目(因为项目的图像和描述正确呈现)。

也就是说,问题在于事件和命令之间。所以,EventToCommand 内部出了点问题。

有人有同样的经历或想法吗?

【问题讨论】:

    标签: c# windows-phone-7 xaml mvvm-light


    【解决方案1】:

    实际上,我最终将 ControlTemplate 移动到单独的 DataTemplate,而不使用样式。

    <DataTemplate x:Key="ProductsListViewerItemTemplate">
        <Grid>
         ****
    

    至少,现在该错误没有重现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 2015-09-26
      • 2013-02-25
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      相关资源
      最近更新 更多