2010-10-28 160 views
0

我已經定義了一個DataGrid這樣的:
禁用上下文菜單WPF

<cc:PEDataGrid AutoGenerateColumns="False" 
       ItemsSource="{Binding Rows}" 
       Width="Auto"     
       PreviewMouseRightButtonDown="PEGrid_PreviewMouseRightButtonDown" 
       Loaded="CommonPEGrid_Loaded">    
     <wpfkit:DataGrid.ContextMenu> 
     <ContextMenu> 
      <MenuItem Header="Cut" /> 
      <MenuItem Header="Copy"/> 
      <MenuItem Header="Paste"/>    
     </ContextMenu> 
    </wpfkit:DataGrid.ContextMenu> 
</cc:PEDataGrid> 

這顯示每一個細胞contextMenu當右鍵點擊完成。
我想要禁用所有的單元格,除了標題,還有一些條件爲的標題的上下文菜單。 (我不想使用,因爲我不想在這裏解釋了一些其他問題DataGridHeaderStyle。)

我已經在數據網格並在處理程序我試圖做類似定義的PreviewMouseRightButtonDown處理程序這::

private void PEGrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     DependencyObject depObj = (DependencyObject)e.OriginalSource; 

     while ((depObj != null) && !(depObj is DataGridColumnHeader)) 
     { 
      depObj = VisualTreeHelper.GetParent(depObj); 
     } 

     if (depObj == null) 
     { 
      return; 
     } 

     if (depObj is DataGridColumnHeader) 
     { 
      //some condition here which says whether contextmenu is required on this header 

      (depObj as DataGridColumnHeader).ContextMenu = null; 
      //the above line is not working!!!! 
     } 
     else 
     { 
      (depObj as DataGridCell).ContextMenu = null; 
       //the above line not working!!!! 
     } 
    } 

我想知道我哪裏錯了!請幫助我解決這個問題。還指導我以更好的方式做,如果我在一個錯誤的方式:)

回答

1

嘿,我解決了它達到我的要求::我修改後的代碼如下所示:::

private void PEGrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    DependencyObject depObj = (DependencyObject)e.OriginalSource; 

    while ((depObj != null) && !(depObj is DataGridColumnHeader)) 
    { 
     depObj = VisualTreeHelper.GetParent(depObj); 
    } 

    if (depObj == null) 
    { 
     return; 
    } 

    if (depObj is DataGridColumnHeader) 
    { 
      dg.ContextMenu.Visibility = Visibility.Visible; //works 
    } 
    else 
    { 
      dg.ContextMenu.Visibility = Visibility.Collapsed; //works 
    } 
} 
+0

我使用此響應作爲停止標題上顯示的上下文菜單的基礎。感謝您發佈最終運作的解決方案。 – coffeecoder 2014-06-18 14:08:28

+0

什麼是dg here..and我們怎樣才能在任何地方調用這個事件.. – Silver 2014-09-25 09:39:14

1

我想這個版本更快,你不必設置上下文菜單的可見性。如果點擊位於列標題上,請取消點擊事件。

private void PEGrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    var depObj = (DependencyObject)e.OriginalSource; 
    while ((depObj != null) && !(depObj is DataGridColumnHeader)) 
    { 
     depObj = VisualTreeHelper.GetParent(depObj); 
    } 
    if (depObj is DataGridColumnHeader) 
    { 
     e.Handled = true; 
    } 
} 
+0

這裏的唯一版本適用於我,如果我在OnMouseRIghtButtonUp事件中使用此代碼。 – KCT 2016-07-27 07:12:05

0

我在DataGridCell中的RichTextBox不是VisualTree元素。我的代碼:

private void dgLog_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     FrameworkContentElement fce = e.OriginalSource as FrameworkContentElement; 
     DependencyObject depObj = e.OriginalSource as DependencyObject; 
     DataGridCell dgc = null; 
     DataGridRow dgr = null; 
     try 
     { 
      while (fce != null && !(fce is DataGridCell)) 
      { 
       depObj = fce.Parent; 
       fce = depObj as FrameworkContentElement; 
      } 
      while (depObj != null && !(depObj is DataGridRow)) 
      { 
       if (depObj is DataGridCell) 
        dgc = depObj as DataGridCell; 
       depObj = VisualTreeHelper.GetParent(depObj); 
      } 
      if (depObj == null) 
       return; 
      dgr = depObj as DataGridRow; 
      if (((LogRecord)dgr.Item).SourceType == "FileMessage" && dgc.Column.DisplayIndex == 1) 
       dgLog.ContextMenu = cm_dgLog; 
      else 
       dgLog.ContextMenu = null; 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(...); 
      TraceHelper.TraceError(ex, GetType()); 
     } 
    }//private void dgLog_PreviewMouseRightButtonDown 

的LogRecord是DataGridColumn.Item

-1
private void dgLog_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     FrameworkContentElement fce = e.OriginalSource as FrameworkContentElement; 
     DependencyObject depObj = e.OriginalSource as DependencyObject; 
     DataGridCell dgc = null; 
     DataGridRow dgr = null; 
     try 
     { 
      while (fce != null && !(fce is DataGridCell)) 
      { 
       depObj = fce.Parent; 
       fce = depObj as FrameworkContentElement; 
      } 
      while (depObj != null && !(depObj is DataGridRow)) 
      { 
       if (depObj is DataGridCell) 
        dgc = depObj as DataGridCell; 
       depObj = VisualTreeHelper.GetParent(depObj); 
      } 
      if (depObj == null) 
       return; 
      dgr = depObj as DataGridRow; 
      if (((LogRecord)dgr.Item).SourceType == "FileMessage" && dgc.Column.DisplayIndex == 1) 
       dgLog.ContextMenu = cm_dgLog; 
      else 
       dgLog.ContextMenu = null; 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(...); 
      TraceHelper.TraceError(ex, GetType()); 
     } 
    }//private void dgLog_PreviewMouseRightButtonDown 

對不起類型。首先嚐試不太好。