简体   繁体   中英

Possible Mvvm-light bug with EventToCommand

Everything starts pretty simple: i have a ListBoxSelector (app is for wp7). Items consists of picture and some text. Items style is stored inside of the StyleTemplate. Image can be tapped, its event is like

<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...

Event itself:

private void OnProductSearchListViewerTapImage(ProductItem item)
    {
        if (item == null) return;

        MessengerInstance.Send<ProductItem, ShoppingCartViewModel>(item);
    }

So far so good. When running, everything works fine... in 95% cases. I got a list of items with proper items, when i'm pressing on some item's image, event is risen, command is sent, correct item is added to the shopping cart.

BUT. In some cases (problems starts, when tapping 24-25-th item), wrong item is added to the shopping cart (list is rendered properly).

  • First what i did - checked command: It holds wrong item. Say, i'm tapping on 24-th item, but inside of command, "item" is first from the list.
  • Second what i did - i grabbed XamlSpy and checked, what is the DataContext of the item. It holds 24-th item, as it should be (because item's image and description is rendered correctly).

That means, the problem is between Event and Command. So, something is wrong inside of EventToCommand.

Anybody have same experience or any ideas?

Actually, i end up moving ControlTemplate to the separate DataTemplate, without using style.

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

At least, now that bug is not reproducing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM