{"record":{"id":"54881fe2efa73ffd","repo":"dotnet/wpf","slug":"sr-clipboardcopymode-disabled","errorCode":null,"errorMessage":"SR.ClipboardCopyMode_Disabled","messagePattern":"SR\\.ClipboardCopyMode_Disabled","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGrid.cs","lineNumber":8216,"sourceCode":"        {\n            args.CanExecute = ClipboardCopyMode != DataGridClipboardCopyMode.None && _selectedCells.Count > 0;\n            args.Handled = true;\n        }\n\n        private static void OnExecutedCopy(object target, ExecutedRoutedEventArgs args)\n        {\n            ((DataGrid)target).OnExecutedCopy(args);\n        }\n\n        /// <summary>\n        /// This virtual method is called when ApplicationCommands.Copy command is executed.\n        /// </summary>\n        /// <param name=\"args\"></param>\n        protected virtual void OnExecutedCopy(ExecutedRoutedEventArgs args)\n        {\n            if (ClipboardCopyMode == DataGridClipboardCopyMode.None)\n            {\n                throw new NotSupportedException(SR.ClipboardCopyMode_Disabled);\n            }\n\n            args.Handled = true;\n\n            // Supported default formats: Html, Text, UnicodeText and CSV\n            Collection<string> formats = new Collection<string>(new string[] { DataFormats.Html, DataFormats.Text, DataFormats.UnicodeText, DataFormats.CommaSeparatedValue });\n            Dictionary<string, StringBuilder> dataGridStringBuilders = new Dictionary<string, StringBuilder>(formats.Count);\n            foreach (string format in formats)\n            {\n                dataGridStringBuilders[format] = new StringBuilder();\n            }\n\n            int minRowIndex;\n            int maxRowIndex;\n            int minColumnDisplayIndex;\n            int maxColumnDisplayIndex;\n\n            // Get the bounding box of the selected cells","sourceCodeStart":8198,"sourceCodeEnd":8234,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGrid.cs#L8198-L8234","documentation":"DataGrid.OnExecutedCopy throws NotSupportedException when the Copy command executes while ClipboardCopyMode is DataGridClipboardCopyMode.None. Copy to clipboard is explicitly disabled, so executing the ApplicationCommands.Copy handler is an unsupported operation.","triggerScenarios":"Invoking Ctrl+C / ApplicationCommands.Copy on a DataGrid whose ClipboardCopyMode is None; calling OnExecutedCopy or raising the Copy command programmatically.","commonSituations":"ClipboardCopyMode=\"None\" set in XAML to block copy, but key bindings still routed to the grid; templates or input bindings still advertise the Copy command.","solutions":["Set ClipboardCopyMode to ExcludeHeader or IncludeHeader to enable copy","Remove/disable the Copy command binding when copy is disabled","Handle the Copy command and set args.Handled=true yourself instead of letting OnExecutedCopy run","Guard code that triggers Copy with a ClipboardCopyMode check"],"exampleFix":"// before\n<CheckBox Checked=\"DisableCopy\"/> <!-- sets ClipboardCopyMode=None but Copy binding remains -->\n// after\nif (grid.ClipboardCopyMode == DataGridClipboardCopyMode.None)\n    grid.CommandBindings.Remove(copyBinding); // or set ClipboardCopyMode=\"ExcludeHeader\"","handlingStrategy":"validation","validationCode":"if (grid.ClipboardCopyMode == DataGridClipboardCopyMode.None)\n    return; // copy disabled; do not execute Copy command","typeGuard":"bool CanCopy(DataGrid g) => g.ClipboardCopyMode != DataGridClipboardCopyMode.None;","tryCatchPattern":"try { grid.GetType().GetMethod(\"OnExecutedCopy\", BindingFlags.NonPublic|BindingFlags.Instance).Invoke(grid, new object[]{ args }); }\ncatch (TargetInvocationException ex) when (ex.InnerException is NotSupportedException) { /* copy disabled */ }","preventionTips":["Keep default ClipboardCopyMode (ExcludeHeader) unless copy must be blocked","Remove Copy command bindings when copy is disabled","Guard programmatic Copy invocations with a ClipboardCopyMode check"],"tags":["wpf","datagrid","clipboard","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}