将事件绑定到ViewModel [英] Bind event to ViewModel
问题描述
View.xaml:
; DataGrid x:Name =employeeGridHeight =250Margin =25,0,10,0ItemsSource ={Binding DetacheringenEmployeesModel}IsReadOnly =TrueColumnHeaderStyle ={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(代码隐藏):
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
}
主要想法是,当我点击DataGrid中的单元格时,事件将触发。我第一次尝试在代码背后,它的工作。事情触发器到目前为止,但是当我调试并单击一个单元格时,我的调试器不会进入该方法。
有没有人知道如何解决这个问题?感谢提前!
PS:当我这样做时,它是否也可以使用(object sender)参数?因为我需要ViewModel中的DataGrid来获取我刚刚点击的ActiveCell。
编辑:
事件绑定与Command一起工作!
我在我的DataGrid中有这样的功能:
< DataGridTextColumn Header =OktWidth =*x:Name =test>
< DataGridTextColumn.ElementStyle>
< Style TargetType ={x:Type TextBlock}>
< Setter Property =TagValue ={Binding Months [9] .AgreementID}/>
如何将Tag属性绑定到ViewModel?我知道它已经从ViewModel绑定了,但是你可以看到这个值来自一个Array / List和每个列的值是不同的。
InvokeCommandAction 需要将 ICommand
绑定到事件处理程序如你所绑定(EmployeeGrid_MouseLeftButtonUp)。
所以你可以在ViewModel中引入一个命令并绑定到它:
查看模型
public ICommand SomeActionCommand {get;组;
XAML:
< i:InvokeCommandAction Command ={Binding SomeActionCommand}/>
I am using WPF and PRISM framework for my application. The pattern I am using is MVVM (Model - View - ViewModel) and I am trying to bring the MouseLeftButtonUp event from the code-behind in the View to the ViewModel (so the event will be according the MVVM rules). For now 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, when I click on a cell in the DataGrid, the event will fire. I first tried it in the code behind, and it worked. I got so far with the EventTriggers, but when I debug and click on a cell, my debugger doesn't come into the method.
Does anyone have an idea how to fix this? Thanks in advance!
PS: Does it also work with the (object sender) parameter when I do it like that? Because I need the DataGrid in my ViewModel to get the ActiveCell I just clicked on.
EDIT:
The event-binding worked with the Command!
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 can I bind the Tag property to the ViewModel? I know it's already bound from the ViewModel, but as you can see the value comes from an Array/List and per column the value is different.
InvokeCommandAction requires the ICommand
to be bound not an event handler as you've bound (EmployeeGrid_MouseLeftButtonUp).
So you can introduce a command in ViewModel and bind to it:
View Model:
public ICommand SomeActionCommand { get; set; }
XAML:
<i:InvokeCommandAction Command="{Binding SomeActionCommand}" />
这篇关于将事件绑定到ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!