如何将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>然后,click-event看起来像这样。我收到一个错误:对象引用未设置为对象的实例
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); }
}发布于 2019-05-23 19:30:44
绑定到父ContextMenu的PlacementTarget
<MenuItem Header="GoToElement"
Click="Click_GoToElement"
Tag="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.Header}"/>https://stackoverflow.com/questions/56273367
复制相似问题