locked
In a DataGrid - how to add a context menu on a column RRS feed

  • Question

  • Where in the xaml do I add a context menu that will show on a column.  My xaml is pretty simple -  but I can't seem to add it at the column level.

     

     

    <DataGrid AutoGenerateColumns="False" Grid.Row="6" Height="126" HorizontalAlignment="Left" Margin="0,1,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="auto" Grid.RowSpan="2" CanUserAddRows="True" CanUserDeleteRows="False" ItemsSource="{Binding TriggerList}" AlternatingRowBackground="Gainsboro" AlternationCount

    ="2">

     

     

    <DataGrid.Columns

    >

     

     

    <DataGridTextColumn Header="Trigger" Binding="{Binding Name}" Width="400">

     

     

    </DataGridTextColumn

    >

     

     

    </DataGrid.Columns

    >

     

     

    </DataGrid

    >

     

    Wednesday, April 13, 2011 8:44 PM

Answers

  • Hi Elizabeth_Zelasky,

    You can use the CellStyle property to set the contextmenu for all DataGridCell in this column.

    Here is a simple sample.

        <DataGridTextColumn Header="Trigger" Binding="{Binding Name}" Width="400">
          <DataGridTextColumn.CellStyle>
            <Style TargetType="DataGridCell">
              <Setter Property="ContextMenu">
                <Setter.Value>
                  <ContextMenu>
                  </ContextMenu>
                </Setter.Value>
              </Setter>
            </Style>
          </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>
    

    Have a nice day.


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Min Zhu Friday, April 15, 2011 2:12 AM
    Thursday, April 14, 2011 3:13 AM
  • You may use DataGridTemplateColumn instead of DataGridTextColumn. The  XAML will look as below

    <DataGridTemplateColumn.CellTemplate>
        			<DataTemplate>
        				<TextBlock Text={Binding Name}>
        		
                  <TextBlock.ContextMenu>
                    <ContextMenu>
                      <MenuItem Header="Copy"/>
                      <MenuItem Header="Paste"/>
                      <MenuItem Header="Clear"/>
                    </ContextMenu>
                  </TextBlock.ContextMenu>
        				</TextBlock>
        			</DataTemplate>
        		</DataGridTemplateColumn.CellTemplate>


    Ajosh Jose
    • Marked as answer by Min Zhu Friday, April 15, 2011 2:12 AM
    Thursday, April 14, 2011 2:33 AM

All replies

  • You may use DataGridTemplateColumn instead of DataGridTextColumn. The  XAML will look as below

    <DataGridTemplateColumn.CellTemplate>
        			<DataTemplate>
        				<TextBlock Text={Binding Name}>
        		
                  <TextBlock.ContextMenu>
                    <ContextMenu>
                      <MenuItem Header="Copy"/>
                      <MenuItem Header="Paste"/>
                      <MenuItem Header="Clear"/>
                    </ContextMenu>
                  </TextBlock.ContextMenu>
        				</TextBlock>
        			</DataTemplate>
        		</DataGridTemplateColumn.CellTemplate>


    Ajosh Jose
    • Marked as answer by Min Zhu Friday, April 15, 2011 2:12 AM
    Thursday, April 14, 2011 2:33 AM
  • Hi Elizabeth_Zelasky,

    You can use the CellStyle property to set the contextmenu for all DataGridCell in this column.

    Here is a simple sample.

        <DataGridTextColumn Header="Trigger" Binding="{Binding Name}" Width="400">
          <DataGridTextColumn.CellStyle>
            <Style TargetType="DataGridCell">
              <Setter Property="ContextMenu">
                <Setter.Value>
                  <ContextMenu>
                  </ContextMenu>
                </Setter.Value>
              </Setter>
            </Style>
          </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>
    

    Have a nice day.


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Min Zhu Friday, April 15, 2011 2:12 AM
    Thursday, April 14, 2011 3:13 AM
  • Thank you for the responses - they have been very helpful!!  I am new xaml and it still is a little strange.

    Thursday, April 14, 2011 1:41 PM
  • XAML grows on you.

    Like a fungus.... Just joking :^)

    It's good stuff XAML and the lookless nature of WPF controls is extremely powerful.

    Thursday, April 14, 2011 1:49 PM
  • Hi Min,

    I have copied your code (answer to Elizabeth_Zelasky about adding a context menu to a DataGridTextColumn) and it works.. that is, it brings up the menu. But how do I handle the click event of the menu? I am getting an error: 

    'Click' is not an event on 'System.Windows.Controls.DataGridTextColumn'.

    Any advice appreciated.

    Wednesday, September 19, 2018 2:14 AM
  • Hi Elizabeth_Zelasky,

    You can use the CellStyle property to set the contextmenu for all DataGridCell in this column.

    Here is a simple sample.

        <DataGridTextColumn Header="Trigger" Binding="{Binding Name}" Width="400">
          <DataGridTextColumn.CellStyle>
            <Style TargetType="DataGridCell">
              <Setter Property="ContextMenu">
                <Setter.Value>
                  <ContextMenu>
                  </ContextMenu>
                </Setter.Value>
              </Setter>
            </Style>
          </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>
    

    Have a nice day.


    Min Zhu [MSFT]

    Hi Min,

    Can you advise how to add an event handler to a ContextMenu created in this way? I'm getting an error saying "Click is not an event on DataGridTextColumn"

    Cheers

    Wednesday, September 19, 2018 9:07 PM