Question

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?

Était-ce utile?

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

Autres conseils

I had a similar problem before, what solved it for me was referencing the VM in the binding. Try to set PassEventArgsToCommand to false if you want to receive the item instead of the EventArgs

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Tap">                                    
         <cmd:EventToCommand PassEventArgsToCommand="False" 
                             CommandParameter="{Binding}" 
                             Command="{Binding Path=VM_Name_Here.Command_Name_Here, Source={StaticResource Locator}}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

Edit- If you are using MVVM Light, in the app.xml you should have something like:

xmlns:vm="clr-namespace:PhoneApplication.ViewModel (namespace where your ViewModelLocator is under)

   <vm:ViewModelLocator x:Key="Locator"
                             d:IsDataSource="true" />

You are telling the binding to look in the ViewModelLocator for a particular VM.

Hope it helps, Regards.

I think it has to do with your binding in the data template. How is the view created? I think the command is a property of the viewModel, not GroupRow1 which is the collection for your items control. You need to bind to the command in your ViewModel. SO if your view is a usercontrol, the following should work. (if its of another type then change the ancestortype)

 <cmd:EventToCommand Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.ItemSelectedCommand}"
    CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBlock}},Path=Name}"/>

The command parameter adds the name of the textblock for example, you could change that to any property of the textblock.

I would personally have SelectedItem property in my viewmodel that can be accesses as an object from the ItemSelectCommand

Hope this helps.

The answer is pretty obvious. In ItemsPanelTemplate your binding source is still the ViewModel and your command stays at your ViewModel. But in DataTemplate you are iterating over GroupRow1 items and your binding source is individual item. If you want to use your command there, you have to bind from the relative source in example:

<i:Interaction.Triggers>
 <i:EventTrigger EventName="Tap">
    <cmd:EventToCommand Command="{Binding ViewModel.ItemSelectedCommand, RelativeSource={RelativeSource AncestorType=UserControl}, Mode=OneWay}"
           PassEventArgsToCommand="True" />
 </i:EventTrigger>
</i:Interaction.Triggers>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top