2017-02-23 92 views
1

我想添加一個contextmenu到Forms.DataGridView。 我的嘗試失敗<wf:MenuItem Text="Save" Click="menuFEInsertLine_Click">告訴我,MenuItem不能添加到MenuItemCollection。此外,該字符串預計。 我怎樣才能讓這個xaml工作?WPF表格DataGridView上下文菜單

<WindowsFormsHost Name="wfhFileEditorWindow" DockPanel.Dock="Top" HorizontalAlignment="Stretch" AllowDrop="True" Background="White" VerticalAlignment="Stretch"> 
    <WindowsFormsHost.Child> 
     <local:DataGridViewEx x:Name="dgFileEditorWindow" AllowDrop="True" BackColor="White" BackgroundColor="White"> 
      <local:DataGridViewEx.ContextMenu> 
       <wf:ContextMenu> 
        <wf:ContextMenu.MenuItems> 
         <wf:MenuItem Text="Save" Click="menuFEInsertLine_Click"> 
          <wf:MenuItem.Icon> 
           <Image Source="star.png"/> 
          </wf:MenuItem.Icon> 
         </wf:MenuItem> 
         <wf:MenuItem Header="Save" Click="menuFEDeleteLine_Click"> 
          <MenuItem.Icon> 
           <Image Source="delete.png"/> 
          </MenuItem.Icon> 
         </wf:MenuItem> 
+0

第二'MenuItem'沒有'WF的圖標('MenuItem.Icon')。 ..你確定這是正確的嗎? – Jcl

+0

這是不對的。我糾正了它,但它仍然在同一個地方休息。 – Adder

回答

0

我工作圍繞這通過重寫DataGridView中的兩種方法:`命名空間:

Private Sub dataGridView_MouseClick(ByVal sender As Object, 
    ByVal e As System.Windows.Forms.MouseEventArgs 
    ) Handles Me.MouseClick 
    If e.Button = Forms.MouseButtons.Right Then 

     Dim m As New Forms.ContextMenu() 

     Dim hi As HitTestInfo = Me.HitTest(e.X, e.Y) 

     If hi.RowIndex >= 0 Then 
      m.MenuItems.Add(New Forms.MenuItem("Insert Line", AddressOf CType(Application.Current.MainWindow, MainWindow).menuFEInsertLine_Click)) 
     End If 

     If hi.ColumnIndex >= 0 Then 
      If hi.RowIndex >= 0 Then 
       m.MenuItems.Add("-") 
      End If 
      m.MenuItems.Add(New Forms.MenuItem("Insert Column", AddressOf CType(Application.Current.MainWindow, MainWindow).menuFEInsertColumn_Click)) 
     End If 

     m.Show(Me, New System.Drawing.Point(e.X, e.Y)) 
    End If 
End Sub 

Private Sub dataGridView_MouseMove(ByVal sender As Object, 
    ByVal e As System.Windows.Forms.MouseEventArgs 
    ) Handles Me.MouseMove 

    Try 
     Dim hi As HitTestInfo = Me.HitTest(e.X, e.Y) 
     If hi IsNot Nothing Then 
      Me.SetCurrentCellAddressCore(hi.ColumnIndex, hi.RowIndex, False, False, False) 
     End If 
    Catch ex As System.ArgumentOutOfRangeException 
    End Try 
End Sub