Using EventToCommand in a DataTemplate

Nov 3, 2010 at 11:15 AM

I am using a EventToCommand in a DataTemplate but cant get it to work as the command doesnt get triggered.

So I guess the issue is that the DataContext of each row in my ItemsControll is the objects in the ItemControls ItemSource. So my command is not recognized.

I get an error in the output window stating

System.Windows.Data Error: BindingExpression path error: 'UpdatedCommand' property not found on 'Plugin.MVVM.ViewModel.EventViewModel'

 

<i:Interaction.Triggers>
     <i:EventTrigger EventName="MouseLeftButtonDown">
          <cmd:EventToCommand Command="{Binding UpdateCommand}"/>
     </i:EventTrigger>
</i:Interaction.Triggers>

 

Could anyone please show me how I can solve this. 

Thanks

 

                        
                            
                        
                    

 

 

Nov 5, 2010 at 9:09 AM
Edited Nov 5, 2010 at 9:12 AM

I solved this using button template instead and it works great. I needed to be able to click on items in my ItemsControll so using a button seems like a better approach anyways.

Here is the code I use incase anyone else has simular problems

 

 <ItemsControl ItemsSource="{Binding Events}" Foreground="White" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
	    <Border Background="{StaticResource ContentBackground}">                                
	       <Button Command="{Binding           
                 MainEvent.EventClickedCommand, Mode=OneWay, 
                 Source={StaticResource Locator}}" 
                 CommandParameter="{Binding Mode=OneWay}" 
		Template="{StaticResource EventControlTemplate}" 
                Foreground="{StaticResource WindowTextColor}"/>   
        </Border>
    </DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

The template looks like this
<ControlTemplate x:Key="EventControlTemplate" TargetType="Button">
    <Grid x:Name="GridContainer" Margin="0,1,0,0" 
                            Background="Transparent" Cursor="Hand">
             ADD YOUR ITEM GUI HERE
    </Grid>		   
</ControlTemplate> 

 

In the datatemplate I use MainEvent witch is defined in the ViewModelLocator

 

       public MainEventViewModel MainEvent
        {
            get
            {
                return Container.Resolve<MainEventViewModel>();
            }
        }      

 

In MainEventViewModel.cs I do the following to detect the click on an item

Property:

 

 public RelayCommand<EventViewModel> EventClickedCommand
        {
            get;
            private set;
        }

 

In Constructor

 

 EventClickedCommand = new RelayCommand<EventViewModel>(model =>                 
//Do work                
));

You can see the EventViewModel is passed as an argument so you can do 

 

EventViewModel.Model.YOURPROPERTY

I also have mouseover/pressed effects on the items. To do this I simply add <VisualStateManager.VisualStateGroups> element to my button template. 

Hope this helps