我有一个DataGrid,其上下文菜单只能从行中访问。我从this StackOverflow Answer得到了解决方案。我需要将上下文菜单绑定到行,因为click方法需要选择特定的行。行上下文菜单看起来很棒。问题是现在菜单项的点击事件没有触发。有人知道如何解决这个问题吗?
XAML:
<DataGrid x:Name="NetworkEntryGrid" Margin="10,10,10,0" CanUserResizeColumns="True" CanUserReorderColumns="False" ColumnWidth="*" IsReadOnly="True" IsManipulationEnabled="False">
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Install As">
<MenuItem x:Name="InstallLabelsMenuItem" Header="Labels" Click="InstallLabelsMenuItem_Click"/>
<MenuItem x:Name="InstallATagsMenuItem" Header="A Tags" Click="InstallATagsMenuItem_Click"/>
<MenuItem x:Name="InstallSpecialLabelsMenuItem" Header="Special Labels" Click="InstallSpecialLabelsMenuItem_Click"/>
<MenuItem x:Name="InstallReportsMenuItem" Header="Reports" Click="InstallReportsMenuItem_Click"/>
<MenuItem x:Name="InstallPawnTicketsMenuItem" Header="Pawn Tickets" Click="InstallPawnTicketsMenuItem_Click"/>
<MenuItem x:Name="InstallReceiptsMenuItem" Header="Receipts" Click="InstallReceiptsMenuItem_Click"/>
</MenuItem>
<MenuItem Header="Copy">
<MenuItem x:Name="CopyNameMenuItem" Header="Name" Click="CopyNameMenuItem_Click"/>
<MenuItem x:Name="CopyIpMenuItem" Header="IP" Click="CopyIpMenuItem_Click"/>
<MenuItem x:Name="CopyMacMenuItem" Header="MAC Address" Click="CopyMacMenuItem_Click"/>
<MenuItem x:Name="CopyVendorMenuItem" Header="Vendor" Click="CopyVendorMenuItem_Click"/>
</MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
</DataGrid>
我在Click事件方法中添加了MessageBox和WriteLine命令以确保它们不会显示。
C#:
private void InstallLabelsMenuItem_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("Labels Clicked");
MessageBox.Show("Labels Clicked", "Menu Item Event", MessageBoxButton.OK);
AssignPrinter("Labels");
}
答案 0 :(得分:1)
将ContextMenu
添加为Resource
,并将其引用为StaticResource
。
<DataGrid x:Name="NetworkEntryGrid" Margin="10,10,10,0" CanUserResizeColumns="True" CanUserReorderColumns="False" ColumnWidth="*" IsReadOnly="True" IsManipulationEnabled="False">
<DataGrid.Resources>
<ContextMenu x:Key="contextMenu">
<MenuItem Header="Install As">
<MenuItem x:Name="InstallLabelsMenuItem" Header="Labels" Click="InstallLabelsMenuItem_Click"/>
<MenuItem x:Name="InstallATagsMenuItem" Header="A Tags" Click="InstallATagsMenuItem_Click"/>
<MenuItem x:Name="InstallSpecialLabelsMenuItem" Header="Special Labels" Click="InstallSpecialLabelsMenuItem_Click"/>
<MenuItem x:Name="InstallReportsMenuItem" Header="Reports" Click="InstallReportsMenuItem_Click"/>
<MenuItem x:Name="InstallPawnTicketsMenuItem" Header="Pawn Tickets" Click="InstallPawnTicketsMenuItem_Click"/>
<MenuItem x:Name="InstallReceiptsMenuItem" Header="Receipts" Click="InstallReceiptsMenuItem_Click"/>
</MenuItem>
<MenuItem Header="Copy">
<MenuItem x:Name="CopyNameMenuItem" Header="Name" Click="CopyNameMenuItem_Click"/>
<MenuItem x:Name="CopyIpMenuItem" Header="IP" Click="CopyIpMenuItem_Click"/>
<MenuItem x:Name="CopyMacMenuItem" Header="MAC Address" Click="CopyMacMenuItem_Click"/>
<MenuItem x:Name="CopyVendorMenuItem" Header="Vendor" Click="CopyVendorMenuItem_Click"/>
</MenuItem>
</ContextMenu>
<Style TargetType="DataGridRow">
<Setter Property="ContextMenu" Value="{StaticResource contextMenu}"/>
</Style>
</DataGrid.Resources>
</DataGrid>