dotnet/wpf · error · ArgumentException

Specified InkCanvasClipboardFormat is not valid.

Error message

Specified InkCanvasClipboardFormat is not valid.

What it means

WPF's InkCanvas ClipboardProcessor builds a map of preferred clipboard formats (XAML or Text clipboard data objects) from InkCanvasClipboardFormat values. A switch over the enum instantiates the matching data object; any value that is neither Xaml nor Text reaches the default case and throws ArgumentException (SR.InvalidClipboardFormat) naming the invalid parameter.

Solutions

  1. Only pass InkCanvasClipboardFormat.Xaml or InkCanvasClipboardFormat.Text
  2. Validate with Enum.IsDefined(typeof(InkCanvasClipboardFormat), value) before adding to the collection
  3. If values come from settings/config, parse defensively and fall back to the default (Xaml) format on unknown values

Example fix

// before
collection.PreferredFormat = (InkCanvasClipboardFormat)savedValue; // savedValue = 7 -> ArgumentException

// after
var format = (InkCanvasClipboardFormat)savedValue;
if (Enum.IsDefined(typeof(InkCanvasClipboardFormat), format))
{
    collection.PreferredFormat = format;
}
else
{
    collection.PreferredFormat = InkCanvasClipboardFormat.Xaml;
}
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(InkCanvasClipboardFormat), value))
    throw new ArgumentOutOfRangeException(nameof(value));

preferredFormat = value;

Type guard

static bool IsValidInkClipboardFormat(InkCanvasClipboardFormat format)
    => format == InkCanvasClipboardFormat.Xaml || format == InkCanvasClipboardFormat.Text;

Try / catch

try
{
    processor.PreferredFormat = value;
}
catch (ArgumentException ex)
{
    // invalid InkCanvasClipboardFormat value; fall back to Xaml
    processor.PreferredFormat = InkCanvasClipboardFormat.Xaml;
}

Prevention

When it happens

Trigger: Adding to ClipboardProcessor's preferred-format collection a value that is not InkCanvasClipboardFormat.Xaml or InkCanvasClipboardFormat.Text — typically an arbitrary int cast to the enum (e.g., (InkCanvasClipboardFormat)7) that skips Enum validation.

Common situations: Restoring clipboard-format preferences from config/persisted settings via raw int casts; reflection-driven property assignment with unvalidated values; assumptions that the enum has more members than it actually does.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Ink/ClipboardProcessor.cs:283

                foreach ( InkCanvasClipboardFormat format in value )
                {
                    // If we find the duplicate format in our preferred list, we should just skip it.
                    if ( !preferredData.ContainsKey(format) )
                    {
                        ClipboardData clipboardData = null;
                        switch ( format )
                        {
                            case InkCanvasClipboardFormat.InkSerializedFormat:
                                clipboardData = new ISFClipboardData();
                                break;
                            case InkCanvasClipboardFormat.Xaml:
                                clipboardData = new XamlClipboardData();
                                break;
                            case InkCanvasClipboardFormat.Text:
                                clipboardData = new TextClipboardData();
                                break;
                            default:
                                throw new ArgumentException(SR.InvalidClipboardFormat, nameof(value));
                        }

                        preferredData.Add(format, clipboardData);
                    }
                }
            
                _preferredClipboardData = preferredData;
            }
        }        


        //-------------------------------------------------------------------------------
        //
        // Private Methods
        //
        //-------------------------------------------------------------------------------

        #region Private Methods

View on GitHub (pinned to 81131a70a4)