This project has moved and is read-only. For the latest updates, please go here.

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:EventTrigger EventName="MouseLeftButtonDown">
          <cmd:EventToCommand Command="{Binding UpdateCommand}"/>


Could anyone please show me how I can solve this. 






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" >
	    <Border Background="{StaticResource ContentBackground}">                                
	       <Button Command="{Binding           
                 MainEvent.EventClickedCommand, Mode=OneWay, 
                 Source={StaticResource Locator}}" 
                 CommandParameter="{Binding Mode=OneWay}" 
		Template="{StaticResource EventControlTemplate}" 
                Foreground="{StaticResource WindowTextColor}"/>   

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


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


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


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



 public RelayCommand<EventViewModel> EventClickedCommand
            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 



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