Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WPF: Context-menu for Row and Header of DataGrid

WPF: Context-menu for Row and Header of DataGrid

Scheduled Pinned Locked Moved WPF
wpfcsharptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Prasoon Chaudhary
    wrote on last edited by
    #1

    I've a dataGrid in my XAML file. I need to display two separate context-menus on right-click on Rows and Header. Having same context-menu on both is easy, but how to have different ones? Will someone please provide a bit of code?

    P 1 Reply Last reply
    0
    • P Prasoon Chaudhary

      I've a dataGrid in my XAML file. I need to display two separate context-menus on right-click on Rows and Header. Having same context-menu on both is easy, but how to have different ones? Will someone please provide a bit of code?

      P Offline
      P Offline
      Prasoon Chaudhary
      wrote on last edited by
      #2

      I think, I've got the answer.

      <!--ContextMenu Definition-->
      <ContextMenu x:Key="cm_rowMenu">
      <!--ContextMenu For Row-->
      </ContextMenu>

      	<ContextMenu  x:Key="cm\_columnHeaderMenu">
      		 <!--ContextMenu For Header-->
      	</ContextMenu>
      

      <!--ContextMenu Definition Ends-->

      <!--Default Header Style-->
      <Style x:Key="DefaultColumnHeaderStyle" TargetType="{x:Type Custom:DataGridColumnHeader}">
      <Setter Property="ContextMenu" Value="{StaticResource cm_columnHeaderMenu}" />
      </Style>

      <!--Default Row Style-->
      <Style x:Key="DefaultRowStyle" TargetType="{x:Type Custom:DataGridRow}">
      <Setter Property="ContextMenu" Value="{StaticResource cm_rowMenu}" />
      </Style>

      <!--Setting styles for DataGrid-->
      <Custom:DataGrid
      RowStyle="{StaticResource DefaultRowStyle}"
      ColumnHeaderStyle="{StaticResource DefaultColumnHeaderStyle}"/>

      P 1 Reply Last reply
      0
      • P Prasoon Chaudhary

        I think, I've got the answer.

        <!--ContextMenu Definition-->
        <ContextMenu x:Key="cm_rowMenu">
        <!--ContextMenu For Row-->
        </ContextMenu>

        	<ContextMenu  x:Key="cm\_columnHeaderMenu">
        		 <!--ContextMenu For Header-->
        	</ContextMenu>
        

        <!--ContextMenu Definition Ends-->

        <!--Default Header Style-->
        <Style x:Key="DefaultColumnHeaderStyle" TargetType="{x:Type Custom:DataGridColumnHeader}">
        <Setter Property="ContextMenu" Value="{StaticResource cm_columnHeaderMenu}" />
        </Style>

        <!--Default Row Style-->
        <Style x:Key="DefaultRowStyle" TargetType="{x:Type Custom:DataGridRow}">
        <Setter Property="ContextMenu" Value="{StaticResource cm_rowMenu}" />
        </Style>

        <!--Setting styles for DataGrid-->
        <Custom:DataGrid
        RowStyle="{StaticResource DefaultRowStyle}"
        ColumnHeaderStyle="{StaticResource DefaultColumnHeaderStyle}"/>

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Yup - that looks like it should do it.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups