Search code examples
silverlightwindows-phone-8mvvm-lightdatatemplateeventtocommand

EventToCommand in ItemControl in Windows Phone 8


I cannot understand why I cant call eventToCommand in my datatemplate inside ItemControl. According to this post I should implement it in the dataTemplate, but the command is never invoked. This is the eventToCommand im trying to insert in my code.

<i:Interaction.Triggers>
   <i:EventTrigger EventName="Tap">
        <cmd:EventToCommand Command="{Binding ItemSelectedCommand, Mode=OneWay}"
               PassEventArgsToCommand="True" />
   </i:EventTrigger>
</i:Interaction.Triggers>

And this is the code im trying to instert it to. However, as the comments say, it is never invoked when put in the dataTemplete. The problem is not the viewModel, the command works in the panelTemplate.

<ItemsControl ItemsSource="{Binding GroupRow1}" >
   <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
         <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
             <!-- COMMAND WORKS HERE, but cannot locate which item has been pressed -->
         </StackPanel>
      </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
       <DataTemplate>
           <Border Background="#FF1756C3" Height="173" Width="173" Margin="12,0,0,0" >
                <!-- COMMAND DOES NOT WORK HERE?!?! -->
                <StackPanel> 
                   <TextBlock Text="{Binding LineOne}" /> <!-- These bindings work -->
                   <TextBlock Text="{Binding LineTwo}" />
                </StackPanel>
           </Border>
       </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

How do I find out which Item has been pressed?


Solution

  • Many answers, but none of them worked for me. I redesigned my solution and got rid of the eventtocommand which wasent working. Instead I made buttons with custom content to look the same as my border.

    Simpler code and a better solution.

    <ItemsControl ItemsSource="{Binding GroupRow1}" >
    <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
         <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
         </StackPanel>
      </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
       <DataTemplate>
          <Button CommandParameter="{Binding LineOne}" Height="195" Width="195" Margin="0,0,-10,0" Click="Button_Click_2" Background="#FF1756C3">
                <StackPanel> 
                   <TextBlock Text="{Binding LineOne}" /> 
                   <TextBlock Text="{Binding LineTwo}" />
                </StackPanel>
           </Button>
       </DataTemplate>
    </ItemsControl.ItemTemplate>
    </ItemsControl>