dotnet/wpf · error · NotSupportedException

SR.ClipboardCopyMode_Disabled

Error message

SR.ClipboardCopyMode_Disabled

What it means

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.

Solutions

  1. Set ClipboardCopyMode to ExcludeHeader or IncludeHeader to enable copy
  2. Remove/disable the Copy command binding when copy is disabled
  3. Handle the Copy command and set args.Handled=true yourself instead of letting OnExecutedCopy run
  4. Guard code that triggers Copy with a ClipboardCopyMode check

Example fix

// before
<CheckBox Checked="DisableCopy"/> <!-- sets ClipboardCopyMode=None but Copy binding remains -->
// after
if (grid.ClipboardCopyMode == DataGridClipboardCopyMode.None)
    grid.CommandBindings.Remove(copyBinding); // or set ClipboardCopyMode="ExcludeHeader"
Defensive patterns

Strategy: validation

Validate before calling

if (grid.ClipboardCopyMode == DataGridClipboardCopyMode.None)
    return; // copy disabled; do not execute Copy command

Type guard

bool CanCopy(DataGrid g) => g.ClipboardCopyMode != DataGridClipboardCopyMode.None;

Try / catch

try { grid.GetType().GetMethod("OnExecutedCopy", BindingFlags.NonPublic|BindingFlags.Instance).Invoke(grid, new object[]{ args }); }
catch (TargetInvocationException ex) when (ex.InnerException is NotSupportedException) { /* copy disabled */ }

Prevention

When it happens

Trigger: Invoking Ctrl+C / ApplicationCommands.Copy on a DataGrid whose ClipboardCopyMode is None; calling OnExecutedCopy or raising the Copy command programmatically.

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/54881fe2efa73ffd. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGrid.cs:8216

        {
            args.CanExecute = ClipboardCopyMode != DataGridClipboardCopyMode.None && _selectedCells.Count > 0;
            args.Handled = true;
        }

        private static void OnExecutedCopy(object target, ExecutedRoutedEventArgs args)
        {
            ((DataGrid)target).OnExecutedCopy(args);
        }

        /// <summary>
        /// This virtual method is called when ApplicationCommands.Copy command is executed.
        /// </summary>
        /// <param name="args"></param>
        protected virtual void OnExecutedCopy(ExecutedRoutedEventArgs args)
        {
            if (ClipboardCopyMode == DataGridClipboardCopyMode.None)
            {
                throw new NotSupportedException(SR.ClipboardCopyMode_Disabled);
            }

            args.Handled = true;

            // Supported default formats: Html, Text, UnicodeText and CSV
            Collection<string> formats = new Collection<string>(new string[] { DataFormats.Html, DataFormats.Text, DataFormats.UnicodeText, DataFormats.CommaSeparatedValue });
            Dictionary<string, StringBuilder> dataGridStringBuilders = new Dictionary<string, StringBuilder>(formats.Count);
            foreach (string format in formats)
            {
                dataGridStringBuilders[format] = new StringBuilder();
            }

            int minRowIndex;
            int maxRowIndex;
            int minColumnDisplayIndex;
            int maxColumnDisplayIndex;

            // Get the bounding box of the selected cells

View on GitHub (pinned to 81131a70a4)