如何将DataRow绑定到与该行关联的ContextMenu.MenuItem的Tag属性? 这是我到目前为止的内容:
<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>
然后,点击事件如下所示。我收到错误:对象引用未设置为对象的实例
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); }
}
答案 0 :(得分:0)
绑定到父级PlacementTarget
的{{1}}:
ContextMenu