Jel*_*ghs 7 c# wpf events xaml mvvm
我正在为我的应用程序使用WPF和PRISM框架.我正在使用的模式是MVVM(Model - View - ViewModel),我试图将ViewLeft中的MouseLeftButtonUp事件从ViewModel引入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>
Run Code Online (Sandbox Code Playgroud)
View.xaml.cs(代码隐藏):
public partial class UC1001_DashBoardConsultants_View
{
public UC1001_DashBoardConsultants_View(UC1001_DashboardConsultantViewModel viewModel)
{
InitializeComponent();
DataContext = viewModel;
}
}
Run Code Online (Sandbox Code Playgroud)
ViewModel.cs:
public void EmployeeGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
// insert logic here
}
Run Code Online (Sandbox Code Playgroud)
主要的想法是,当我点击DataGrid中的一个单元格时,事件将会触发.我首先在后面的代码中尝试了它,并且它有效.我到目前为止使用EventTriggers,但是当我调试并单击一个单元格时,我的调试器没有进入该方法.
有谁知道如何解决这个问题?提前致谢!
PS:当我这样做时,它是否也与(对象发送者)参数一起使用?因为我需要在我的ViewModel中使用DataGrid来获取我刚刚点击的ActiveCell.
编辑:
事件绑定与Command一起工作!
我在我的DataGrid中有这个:
<DataGridTextColumn Header="Okt" Width="*" x:Name="test" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Tag" Value="{Binding Months[9].AgreementID}"/>
Run Code Online (Sandbox Code Playgroud)
如何将Tag属性绑定到ViewModel?我知道它已经从ViewModel绑定了,但是你可以看到值来自一个数组/列表和每列的值是不同的.
sll*_*sll 10
InvokeCommandAction要求ICommand
绑定不是您绑定的事件处理程序(EmployeeGrid_MouseLeftButtonUp).
所以你可以在ViewModel中引入一个命令并绑定到它:
查看型号:
public ICommand SomeActionCommand { get; set; }
Run Code Online (Sandbox Code Playgroud)
XAML:
<i:InvokeCommandAction Command="{Binding SomeActionCommand}" />
Run Code Online (Sandbox Code Playgroud)