简体   繁体   English

WPF DataGrid 行上的 ContextMenu - 事件未触发

[英]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..我已经搜索了很多关于这个主题的内容。我不得不说我对 WPF 很陌生。

here is my question: I have a datagrid and need a contextmenu at the datagrid-Row.这是我的问题:我有一个数据网格,需要在 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!氪!

You do not seem to have added any event handler to the MenuItem so it does nothing.您似乎没有向MenuItem添加任何事件处理程序,因此它什么也不做。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM