FiorinaN
FiorinaN

Reputation: 63

ContextMenu on WPF DataGrid Row - Event does not trigger

I've searched a lot about this topic.. I have to say I'm quite new to WPF..

here is my question: I have a datagrid and need a contextmenu at the datagrid-Row.

I have this code:

<Grid>  
   <TabControl>
      <TabItem>
         <Grid>
            <DataGrid Grid.Row="2" Grid.ColumnSpan="3" AutoGenerateColumns="False" HorizontalAlignment="Stretch" Margin="6,6,0,0" Name="gridErrors" 
                          VerticalAlignment="Stretch" AlternatingRowBackground="Gainsboro"  AlternationCount="2" SelectionChanged="gridErrors_SelectionChanged"
                          IsEnabled="False">
                  <DataGrid.Resources>
                        <ContextMenu x:Key="DataRowContextMenu">
                            <MenuItem Header="Detaillierte Fehlerbehandlung" />
                        </ContextMenu>
                    </DataGrid.Resources>

                    <DataGrid.RowStyle>
                        <Style TargetType="{x:Type DataGridRow}" x:Name="styleForDataRow">
                            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="OnDataGridRow_PreviewMouseLeftButtonDown" />
                            <Setter Property="ContextMenu" Value="{StaticResource DataRowContextMenu}">
                                <!--<Setter.Value>
                                    <ContextMenu>
                                        <MenuItem Header="Detaillierte Fehlerbehandlung" />                                            
                                    </ContextMenu>
                                </Setter.Value>-->
                            </Setter>                               
                        </Style>
                    </DataGrid.RowStyle>

                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding Number}" Header="Input Nummer" IsReadOnly="True"></DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding Error}" Header="Meldung" IsReadOnly="True"></DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding DetailInfo}" Header="Detail" IsReadOnly="True"></DataGridTextColumn>
                    </DataGrid.Columns>
                </DataGrid>
      </TabItem>
   </TabControl>
</Grid>

This works fine, I can see the Context Menu by right clickin on a row, but the only thing is that the Event is not fired (only when I left-clik in the Grid itself, not with the Context Menu)

I would appreciate it if anybody can help me.. it would save my day :-))

Thanks a lot in advance!

kr!

Upvotes: 4

Views: 9564

Answers (1)

brunnerh
brunnerh

Reputation: 185057

You do not seem to have added any event handler to the MenuItem so it does nothing.

<ContextMenu x:Key="DataRowContextMenu">
    <MenuItem Header="Detaillierte Fehlerbehandlung"
              Click="MyClickEventHandler"/>
</ContextMenu>

Upvotes: 4

Related Questions