首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从DataRow.ContextMenu事件中检索DataRow

如何从DataRow.ContextMenu事件中检索DataRow
EN

Stack Overflow用户
提问于 2019-05-23 18:36:53
回答 1查看 41关注 0票数 1

如何将DataRow绑定到与该行关联的ContextMenu.MenuItem的Tag属性?这是我到目前为止所知道的:

代码语言:javascript
复制
<DataGrid.Resources>
    <ContextMenu x:Key="RowContextMenu">
        <ContextMenu.Items>
            <!--This line doesnot work-->
            <MenuItem Header="GoToElement" Click="Click_GoToElement" Tag="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=Row.Header}"/>
        </ContextMenu.Items>
    </ContextMenu>
</DataGrid.Resources>

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow" BasedOn="{StaticResource {x:Type DataGridRow}}">
        <Setter Property="ContextMenu" Value="{StaticResource RowContextMenu}" />
    </Style>
</DataGrid.RowStyle>

然后,click-event看起来像这样。我收到一个错误:对象引用未设置为对象的实例

代码语言:javascript
复制
private void Click_GoToElement(object sender, RoutedEventArgs e)
{
    try
    {
        var row = ((System.Windows.Controls.MenuItem)sender).Tag;
        MessageBox.Show(row.ToString());
    }
    catch (Exception ex) { MessageBox.Show(ex.Message + "\n" + ex.StackTrace); }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-23 19:30:44

绑定到父ContextMenuPlacementTarget

代码语言:javascript
复制
<MenuItem Header="GoToElement"
          Click="Click_GoToElement"
          Tag="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.Header}"/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56273367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档