DataTemplate中的EventToCommand [英] EventToCommand in DataTemplate
问题描述
我正在使用MVVM-light-Toolkit中的EventToCommand类来处理WPF-DataGrid中的AutoGeneratingColumn-Event.它在我的Main-DataGrid中可以正常工作,但是我在RowDetailsTemplate中使用了另一个DataGrid,在这里我遇到了一个问题: AutoGeneratingColumn在EventToCommand-Object生成之前触发.有解决这个问题的方法吗? 这是我的Xaml代码的一部分:
I'm using the EventToCommand Class from the MVVM-light-Toolkit to handle the AutoGeneratingColumn-Event in the WPF-DataGrid. It works fine in my Main-DataGrid, but I use another DataGrid in the RowDetailsTemplate and here I got a problem: The AutoGeneratingColumn fires before the EventToCommand-Object was generated. Is there a solution for this problem? Here is a piece of my Xaml-Code:
<DataGrid DockPanel.Dock="Top" AutoGenerateColumns="True" Name="table" VerticalAlignment="Top" ItemsSource="{Binding PartBatchList}" IsReadOnly="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="AutoGeneratingColumn">
<hgc:EventToCommand Command="{Binding AutoGeneratingColumnCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Margin="30,0,30,30" Orientation="Vertical">
<Border CornerRadius="4" Padding="5" Background="White">
<DataGrid ItemsSource="{Binding Workpieces}"
CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False"
AutoGenerateColumns="True" AutoGeneratingColumn="WorkpieceListAutoGeneratingColumn">
<i:Interaction.Triggers>
<i:EventTrigger EventName="AutoGeneratingColumn">
<hgc:EventToCommand Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid},AncestorLevel=2}, Path=DataContext.AutoGeneratingColumnCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
</Border>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
代码隐藏文件中的事件处理程序WorkpieceListAutoGeneratingColumn被调用,而我的ViewModel中的命令从不被调用.
The Event-Handler WorkpieceListAutoGeneratingColumn in the Code-Behind File is called, the Command in my ViewModel is never called.
安德烈亚斯(Andreas)
Andreas
推荐答案
原因应该是您不能在相同的对象/事件组合上具有事件andler和命令事件.从您的DataGrid中删除AutoGeneratingColumn="WorkpieceListAutoGeneratingColumn"
,应调用该命令.
我自己曾经遇到问题:-)
Had the problem once myself :-)
修改
如果后面的代码中需要事件处理程序,请删除EventToCommand
并在后面的代码中调用命令,例如
If you need the eventhandler in the code behind, remove the EventToCommand
and call the command in your code behind, e.g.
public void WorkpieceListAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs args) {
var vm = ((YourViewModel) this.DataContext);
if (vm.AutoGeneratingColumnCommand.CanExecute(eventArgs))
vm.AutoGeneratingColumnCommand.Execute(eventArgs);
}
但是,我认为第一个选择是更好的选择.
编辑2
好,环顾四周,看来<i:Interaction.Triggers/>
仅在对象已渲染并且发生用户交互之后才能起作用(因此命名?).好吧,这意味着仅存在一些事件-在对象构造期间调用的事件-EventToCommand
机制无法处理.在这种情况下,可以使用后面的代码从那里调用您的命令,请参阅我的第一次编辑.
OK, had some look around and it seems that <i:Interaction.Triggers/>
only works after the object is already rendered and user interaction takes place (hence the name?). Well, this means that there are simply just some events - the ones that are called during the construction of the object - that cannot be handled by the EventToCommand
mechanism. In these cases it is OK to use code behind to call your command from there, see my first edit.
这篇关于DataTemplate中的EventToCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!