简体   繁体   中英

Windows Phone 7 - Can not trigger the event from ViewModel.

I want to write the event for list box from View Model. I try like this:-

 <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="Gray" Padding="5" BorderThickness="1">
                        <StackPanel Orientation="Horizontal">
                            <Border BorderBrush="Wheat" BorderThickness="1">
                                <Image  Name="ListPersonImage" Source="{Binding PersonImage}" Height="100" Width="100" Stretch="Uniform" Margin="10,0,0,0"/>
                            </Border>
                            <TextBlock Text="{Binding FirstName}" Name="firstName" Width="200" Foreground="White" Margin="10,10,0,0" FontWeight="SemiBold" FontSize="22"  />

                            <Button DataContext="{Binding DataContext, ElementName=listBox1}" Command="{Binding addPerson}" Height="80" Width="80" >
                                <Button.Background>
                                    <ImageBrush  ImageSource="{Binding imagePath,  Converter={StaticResource pathToImageConverter}}" Stretch="Fill" />
                                </Button.Background>
                            </Button>                               
                        </StackPanel>
                    </Border>
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Tap">
                            <i:InvokeCommandAction Command="{Binding ItemSelectedCommand,Mode=OneWay}" CommandParameter="{Binding}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

My ViewModel:-

  public RelayCommand<MVVMListBoxModel> ItemSelectedCommand { get; private set; }
 public MVVMListBoxViewModel()
        {
           ItemSelectedCommand = new RelayCommand<MVVMListBoxModel>(ItemSelected);
        }

 private void ItemSelected(MVVMListBoxModel myItem)
        {
            MessageBox.Show("Name==>" + myItem.FirstName);
            //throw new NotImplementedException();
        }

But nothing happening. Please let me know where I did mistake. Thanks in advance.

Check output window to see if you got binding error. It seems that you got one, because you have ItemSelectedCommand defined in MVVMListBoxViewModel but ListBoxItem 's DataContext is corresponding MVVMListBoxModel , so binding engine couldn't find the command.

Try to move definition of ItemSelectedCommand to MVVMListBoxModel and see if message box get displayed this way.

Do you want the trigger to be upon the SelectionChanged of the Listbox Item? Then the trigger should be outside the <ListBox.ItemTemplate> ... </ListBox.ItemTemplate> .

And the trigger should bind the CommandParamter to the SelectedItem

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Tap">
        <i:InvokeCommandAction Command="{Binding ItemSelectedCommand,Mode=OneWay}" CommandParameter="{Binding SelectedItem}"/>
     </i:EventTrigger>
</i:Interaction.Triggers>

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