Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firing a Command within EventTrigger of a style?

Tags:

wpf

xaml

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,

like image 298
Houman Avatar asked May 11 '11 09:05

Houman


1 Answers

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>
like image 127
CodeNaked Avatar answered Oct 13 '22 00:10

CodeNaked