Firing a Command within EventTrigger of a style?
As you know you can't bind an Event directly to a command without a behaviour:
<DataGrid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseDoubleClick">
<i:InvokeCommandAction Command="{Binding TradeEntryCommand"} />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
This works perfectly fine, however now I have to refactor this from double clicking the DataGrid itself to double clicking the Cell. (I don't care which cell was clicked)
I was hoping to define this behviour now inside the Cell Style like this:
<Style 开发者_开发知识库x:Key="DefaultCellStyleBase" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="PreviewMouseDoubleClick">
?????????
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<!-- ... -->
</Style>
But how would I bring in the behaviour from above to fire the command?
Highly appreciated,
Since you are retemplating the DataGridCell, you could add the triggers to the root element in the control template. Something like:
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid x:Name="root" Background="Transparent">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseDoubleClick">
<i:InvokeCommandAction Command="{Binding TradeEntryCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Grid>
</ControlTemplate>
Thats a version I am using for a Button-command in a similar situation (Button in DataGridRow, Command on DataGrid should be invoked by the Button and I need the DataContext of the row in my command). You would have to use the command of the InvokeCommandAction of the doubleClick-trigger instead, but then it should work as well, I suppose.
Good luck!
<DataTemplate>
<TextBlock>
<Button x:Name="cmdButton"
Command="{Binding Path=DataContext.CommandNameInViewModel,
RelativeSource={RelativeSource AncestorType={x:Type TypeOfAncestorWithTheViewModel}}}"
CommandParameter="{Binding}" >
Do something
</Button>
</TextBlock>
</DataTemplate>
精彩评论