0
我有一個DataGrid,其上下文菜單隻能從行中訪問。我從this StackOverflow Answer得到了解決方案。我需要將上下文菜單綁定到行,因爲點擊方法需要選擇特定的行。行上的上下文菜單很好看。問題是現在菜單項的點擊事件不會觸發。有人知道如何解決這個問題嗎?WPF DataGridRow ContextMenu MenuItem單擊不觸發事件
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>
我添加一個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");
}
將它標記爲答案,如果它可以幫助你 –