绑定事件视图模型 - WPF [英] Bind event to ViewModel - WPF

查看:265
本文介绍了绑定事件视图模型 - WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF和PRISM框架我的申请。我使用的模式是MVVM(模型 - 视图 - 视图模型),我试图把从代码隐藏的MouseLeftButtonUp事件观对视图模型(因此事件将根据MVVM规则)。现在我有这样的:

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:

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(代码隐藏):

View.xaml.cs (code-behind):

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



ViewModel.cs:

ViewModel.cs:

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

主要的想法是,当我点击DataGrid中的一个单元格,该事件将触发。我第一次尝试在它后面的代码,和它的工作。我与EventTriggers走到这一步,但是当我调试,并单击单元格,我的调试器不会进入的方法。

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:这是否也与(对象发件人)参数工作,当我这样做呢?因为我需要在DataGrid在我的ViewModel获得ActiveCell我刚刚点击

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.

编辑:

本次活动结合与指挥工作。

The event-binding worked with the Command!

我有这在我的DataGrid中:

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 要求的ICommand 来约束并不像你所绑定的事件处理程序(EmployeeGrid_MouseLeftButtonUp)

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:

浏览模式:

public ICommand SomeActionCommand { get; set; }



XAML:

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

这篇关于绑定事件视图模型 - WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆