【发布时间】:2011-12-17 07:03:44
【问题描述】:
我正在为我的应用程序使用 WPF 和 PRISM 框架。我使用的模式是 MVVM (Model - View - ViewModel),我试图将 MouseLeftButtonUp 事件从 View 中的代码隐藏带到 ViewModel (因此该事件将符合 MVVM 规则)。现在我有这个:
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(代码隐藏):
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 中的一个单元格时,将触发该事件。我首先在后面的代码中尝试了它,它起作用了。到目前为止,我已经使用 EventTriggers,但是当我调试并单击一个单元格时,我的调试器没有进入该方法。
有人知道如何解决这个问题吗?提前致谢!
PS:当我这样做时,它是否也适用于 (object sender) 参数?因为我需要 ViewModel 中的 DataGrid 来获取我刚刚点击的 ActiveCell。
编辑:
事件绑定与命令一起使用!
我的 DataGrid 中有这个:
<DataGridTextColumn Header="Okt" Width="*" x:Name="test" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Tag" Value="{Binding Months[9].AgreementID}"/>
如何将 Tag 属性绑定到 ViewModel?我知道它已经从 ViewModel 绑定,但正如您所见,该值来自一个 Array/List 并且每列的值不同。
【问题讨论】: