【问题标题】:ContextMenu on WPF DataGrid Row - Event does not triggerWPF DataGrid Row 上的 ContextMenu - 事件未触发
【发布时间】:2011-07-04 12:06:57
【问题描述】:

我已经搜索了很多关于这个主题..我不得不说我对 WPF 很陌生..

这是我的问题: 我有一个数据网格,需要数据网格行的上下文菜单。

我有这个代码:

<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>

这很好用,我可以通过右键单击一行来查看上下文菜单,但唯一的问题是没有触发事件(仅当我在网格本身中左键单击时,而不是使用上下文菜单)

如果有人可以帮助我,我将不胜感激。它会节省我的一天:-))

提前非常感谢!

韩币!

【问题讨论】:

    标签: c# wpf xaml datagrid contextmenu


    【解决方案1】:

    您似乎没有向MenuItem 添加任何事件处理程序,因此它什么也不做。

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

    【讨论】:

    • 哈,没错!!它正在工作。我已经删除了事件设置器,因为 MenuItem 事件不再需要它了。我对 EventSetter 非常固定。非常感谢!
    • 如果您使用的是 WPF 而不是 Silverlight,则 Command 可能比 click 事件处理程序更强大。
    猜你喜欢
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多