2012-12-06 68 views

回答

1

您可以指定一個ContextMenu到DataGrid本身的「空白」區域,但它也將出現您右鍵任何行點擊,如果你沒有一個不同的上下文菜單分配給行(或「DataGrid.RowStyle」屬性)。

'解決方法'是專門爲行分配不同的上下文菜單。

<DataGrid> 
    <DataGrid.ContextMenu> 
     <ContextMenu> 
      <MenuItem Header="Menu Item in Blank Area" /> 
     </ContextMenu> 
    </DataGrid.ContextMenu> 
    <DataGrid.RowStyle> 
     <Style TargetType="{x:Type DataGridRow}"> 
      <Setter Property="ContextMenu"> 
       <Setter.Value> 
        <ContextMenu> 
         <MenuItem Header="Menu Item just for Populated Rows" /> 
        </ContextMenu> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </DataGrid.RowStyle> 
</DataGrid> 
+0

的理念是相同的上下文菜單分配給細胞並沒有填充細胞(標記爲「本」上的圖像)的區域。和其他上下文菜單到標題。這就是爲什麼我問這個問題。 – MikroDel

+0

請更新您的原始問題以反映這一點。答案仍然是一樣的;您將爲元素創建一個上下文菜單(這將影響DataGrid中的所有元素,包括空白區域和單元格),併爲您要覆蓋的特定事物(在此情況下爲Header)指定一個不同的元素, – BTownTKD

1

基於對前面的回答您的意見,我知道你想設置一個文本菜單上你的頭,和一個獨立的一個爲你的細胞和空的空間。我能夠通過重寫模板來設置單獨ContextMenus:

<Window x:Class="Apc_System_Generator.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Grid.Resources> 
      <ResourceDictionary> 
       <ContextMenu x:Key="cmenu"> 
        <MenuItem Header="FOO" /> 
        <MenuItem Header="BAR" /> 
       </ContextMenu> 
       <ContextMenu x:Key="omenu"> 
        <MenuItem Header="LOL" /> 
        <MenuItem Header="WUT" /> 
       </ContextMenu> 
      </ResourceDictionary> 
     </Grid.Resources> 
     <DataGrid Name="LOL" AutoGenerateColumns="True" ContextMenu="{StaticResource cmenu}"> 
      <DataGrid.Template> 
       <ControlTemplate TargetType="{x:Type DataGrid}"> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="Auto" /> 
          <RowDefinition Height="*" /> 
         </Grid.RowDefinitions> 
         <DataGridColumnHeadersPresenter Grid.Row="0" ContextMenu="{StaticResource omenu}" /> 
         <DataGridRowsPresenter Background="gray" Grid.Row="1" IsItemsHost="True" /> 
        </Grid> 
       </ControlTemplate> 
      </DataGrid.Template> 
     </DataGrid> 
    </Grid> 
</Window>