Bind event to ViewModel

I use the WPF framework and PRISM for my application. I am using the MVVM template (Model - View - ViewModel) and I am trying to cast the MouseLeftButtonUp event from the code in the view in the ViewModel (so that the event will comply with the MVVM rules). At the moment I have this:

View.xaml:

<DataGrid x:Name="employeeGrid" Height="250" Margin="25,0,10,0" ItemsSource="{Binding DetacheringenEmployeesModel}" IsReadOnly="True" ColumnHeaderStyle="{DynamicResource CustomColumnHeader}" AutoGenerateColumns="False" RowHeight="30"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseLeftButtonUp"> <i:InvokeCommandAction Command="{Binding EmployeeGrid_MouseLeftButtonUp}" /> </i:EventTrigger> </i:Interaction.Triggers> <DataGrid.Columns> 

View.xaml.cs (code-behind):

 public partial class UC1001_DashBoardConsultants_View { public UC1001_DashBoardConsultants_View(UC1001_DashboardConsultantViewModel viewModel) { InitializeComponent(); DataContext = viewModel; } } 

ViewModel.cs:

  public void EmployeeGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { // insert logic here } 

The main idea is that when I click on a cell in a DataGrid, the event fires. At first I tried it in code and it worked. I got to EventTriggers, but when I debug and click on a cell, my debugger does not come into this method.

Does anyone have an idea how to fix this? Thanks in advance!

PS: Does it also work with a parameter (object sender) when I do it like this? Since I need a DataGrid in my ViewModel to get the ActiveCell I just clicked on.

EDIT:

Event binding worked with the team!

I have this in my DataGrid:

 <DataGridTextColumn Header="Okt" Width="*" x:Name="test" > <DataGridTextColumn.ElementStyle> <Style TargetType="{x:Type TextBlock}"> <Setter Property="Tag" Value="{Binding Months[9].AgreementID}"/> 

How to associate a Tag property with a ViewModel? I know that it is already associated with the ViewModel, but since you can see that the value is obtained from the array / list and for each column, the value is different.

+7
source share
1 answer

InvokeCommandAction requires an ICommand binding of not an event handler because you are bound (EmployeeGrid_MouseLeftButtonUp).

So, you can enter the command in the ViewModel and bind to it:

Show model:

 public ICommand SomeActionCommand { get; set; } 

XAML:

 <i:InvokeCommandAction Command="{Binding SomeActionCommand}" /> 
+10
source

All Articles