3

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

 2 years ago
source link: https://www.codesd.com/item/windows-phone-7-can-not-trigger-the-event-from-viewmodel.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

advertisements

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.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK