Hi,
Is there any direct way of creating context menu on Silverlight XamGrid, one way i found out is by extending as shown on
http://es.infragistics.com/community/blogs/devin_rader/archive/2011/04/15/extending-xamgrid-with-a-right-click-context-menu.aspx
But apart from this is there any other way of achieving this, without extending.
-Abhi
Hello Abhijeet,
You could restrict the context menu from appearing when right clicking anywhere but on the DataRow of the xamGrid by handling the Opening event of the xamContextMenu:
<ig:ContextMenuManager.ContextMenu>
<ig:XamContextMenu Name="contextMenu1" Opening="contextMenu1_Opening">
<ig:XamMenuItem Header="Item 1" Click="XamMenuItem_Click"/>
<ig:XamMenuItem Header="Item 2" Click="XamMenuItem_Click_1"/>
<ig:XamMenuItem Header="Item 3" Click="XamMenuItem_Click_2"/>
</ig:XamContextMenu>
</ig:ContextMenuManager.ContextMenu>
Then you could if the clicked element is null or not a DataRow and cancel the opening event. This would not allow the context menu to be displayed.
private void contextMenu1_Opening(object sender, OpeningEventArgs e)
{
Row row = (from x in e.GetClickedElements<CellsPanel>()
select x.Row).FirstOrDefault() as Row;
CellControl cell = e.GetClickedElements<CellControl>().FirstOrDefault();
if (cell != null && cell.Column is FillerColumn)
e.Cancel = true;
// Discard displaying the context menu when not clicking on data rows:
if (row == null || row.RowType != RowType.DataRow)
}
More information on this approach you could find in this forum thread. I have added verification whether the cell that is clicked is part of the FillerColumn. If that is true, I cancel the context menu opening. This restricts the menu from showing if the right mouse button is pressed over the last empty column in my example.
In order to get the row on which the menu appears, you could use the “row” variable from the above code snippet.
Please feel free to let me know if you have any other questions.
Thanks,
This one looks ok, two more points from this
1> Is there any way to restrict the menu to appear only on data row (It should not popup, if clicked on Column or row header, and also not show if clicked on empty area of grid.
2> Also how can i get the row on which menu appeared.
-Abhi.
In order to add the context menu to the XamGrid you just have to include the code snippet I provided to the grid’s declaration. Please have a look into the attached sample and let me know if you have any other questions.
I want the context menu to be available on xamgrid.
How can i attach the above context menu to XamGrid.
I have been looking into your post and I am not completely sure what you mean by “direct way of creating context menu”. If you want just to add context menu and add event hadlers for its items, you could use the ContextMenuService to add the menu items and handle there click events.
<ig:ContextMenuService.Manager>
<ig:ContextMenuManager ModifierKeys="None">
<ig:XamContextMenu>
</ig:ContextMenuManager>
</ig:ContextMenuService.Manager>
Please let me know if this helps you or provide me some more details of what you are trying to achieve. Thank you.