Category / Section
How to disable the context menu in WinRT DataGrid for the textbox in edit mode?
2 mins read
The ContextMenu can be disabled for a TextBox in edit mode by deriving a new class from GridCellTextBoxRenderer and overriding the OnWireEditUIElement where ContextMenuOpening event of TextBox could be handled. In WPF we need to set ContextMenu as null in OnWireEditUIElemnt otherwise ContextMenuOpening event is not fired, it’s behavior of Framework.
C#
public sealed partial class MainPage : Page { public MainPage() { InitializeComponent(); //Remove existing TextBox Renderer this.sfdatagrid.CellRenderers.Remove("TextBox"); // Add customized TextBox Renderer. this.sfdatagrid.CellRenderers.Add("TextBox", new CustomizedGridCellTextBoxRenderer()); } } public class CustomizedGridCellTextBoxRenderer : GridCellTextBoxRenderer { protected override void OnWireEditUIElement(TextBox uiElement) { base.OnWireEditUIElement(uiElement); #if WPF uiElement.ContextMenu = null; #else //hook ContextMenuOpening Event of TextBox uiElement.ContextMenuOpening += UiElement_ContextMenuOpening; #endif } private void UiElement_ContextMenuOpening(object sender, ContextMenuEventArgs e) { //Handle the Event e.Handled = true; } protected override void OnUnwireEditUIElement(TextBox uiElement) { base.OnUnwireEditUIElement(uiElement); #if !WPF //Unhook ContextMenuOpening Event of TextBox uiElement.ContextMenuOpening -= UiElement_ContextMenuOpening; #endif } }
Note:
This can also be done for GridNumericColumn by deriving a new class from GridCellNumericRenderer.
Sample Links