数据模板中的 EventToCommand
我使用 MVVM-light-Toolkit 中的 EventToCommand 类来处理 WPF-DataGrid 中的 AutoGenerateColumn-Event。它在我的 Main-DataGrid 中工作正常,但我在 RowDetailsTemplate 中使用另一个 DataGrid,在这里我遇到了问题: AutoGenerateColumn 在生成 EventToCommand-Object 之前触发。这个问题有解决办法吗? 这是我的 Xaml 代码的一部分:
<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>
代码隐藏文件中的事件处理程序 WorkpieceListAutoGenerateColumn 被调用,我的 ViewModel 中的命令从未被调用。
安德烈亚斯
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>
The Event-Handler WorkpieceListAutoGeneratingColumn in the Code-Behind File is called, the Command in my ViewModel is never called.
Andreas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原因应该是你不能在同一个对象/事件组合上有一个事件处理程序和一个事件命令。从 DataGrid 中删除AutoGenerateColumn="WorkpieceListAutoGenerateColumn"
并调用该命令。我自己也遇到过这个问题:-)编辑
如果您需要后面代码中的事件处理程序,请删除
EventToCommand
并调用命令在你的代码后面,例如但是,我认为第一个选项是更好的。编辑 2
好的,环顾四周,似乎
仅在对象已渲染且用户交互发生后才起作用(因此得名?)。嗯,这意味着只有一些事件(在对象构造期间调用的事件)无法由EventToCommand
机制处理。在这些情况下,可以使用隐藏代码从那里调用您的命令,请参阅我的第一次编辑。The reason should be that you can't have an event andler and an event to command on the same object/event combination. Remove theAutoGeneratingColumn="WorkpieceListAutoGeneratingColumn"
from your DataGrid an the command should be called.Had the problem once myself :-)Edit
If you need the eventhandler in the code behind, remove the
EventToCommand
and call the command in your code behind, e.g.But, I think that the first option is the better one.Edit 2
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 theEventToCommand
mechanism. In these cases it is OK to use code behind to call your command from there, see my first edit.您不必在后面使用邪恶的代码;-) 您可以使用附加行为来执行此操作...
然后在 XAML 中使用它,如下所示...
You don't have to use evil code behind ;-) You can do this using an attached behaviour...
Then use it in XAML like this...