{"record":{"id":"d796b81675f199f2","repo":"dotnet/wpf","slug":"specified-inkcanvasclipboardformat-is-not-valid","errorCode":null,"errorMessage":"Specified InkCanvasClipboardFormat is not valid.","messagePattern":"Specified InkCanvasClipboardFormat is not valid\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Ink/ClipboardProcessor.cs","lineNumber":283,"sourceCode":"                foreach ( InkCanvasClipboardFormat format in value )\n                {\n                    // If we find the duplicate format in our preferred list, we should just skip it.\n                    if ( !preferredData.ContainsKey(format) )\n                    {\n                        ClipboardData clipboardData = null;\n                        switch ( format )\n                        {\n                            case InkCanvasClipboardFormat.InkSerializedFormat:\n                                clipboardData = new ISFClipboardData();\n                                break;\n                            case InkCanvasClipboardFormat.Xaml:\n                                clipboardData = new XamlClipboardData();\n                                break;\n                            case InkCanvasClipboardFormat.Text:\n                                clipboardData = new TextClipboardData();\n                                break;\n                            default:\n                                throw new ArgumentException(SR.InvalidClipboardFormat, nameof(value));\n                        }\n\n                        preferredData.Add(format, clipboardData);\n                    }\n                }\n            \n                _preferredClipboardData = preferredData;\n            }\n        }        \n\n\n        //-------------------------------------------------------------------------------\n        //\n        // Private Methods\n        //\n        //-------------------------------------------------------------------------------\n\n        #region Private Methods","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Ink/ClipboardProcessor.cs#L265-L301","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Only pass InkCanvasClipboardFormat.Xaml or InkCanvasClipboardFormat.Text","Validate with Enum.IsDefined(typeof(InkCanvasClipboardFormat), value) before adding to the collection","If values come from settings/config, parse defensively and fall back to the default (Xaml) format on unknown values"],"exampleFix":"// before\ncollection.PreferredFormat = (InkCanvasClipboardFormat)savedValue; // savedValue = 7 -> ArgumentException\n\n// after\nvar format = (InkCanvasClipboardFormat)savedValue;\nif (Enum.IsDefined(typeof(InkCanvasClipboardFormat), format))\n{\n    collection.PreferredFormat = format;\n}\nelse\n{\n    collection.PreferredFormat = InkCanvasClipboardFormat.Xaml;\n}","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(InkCanvasClipboardFormat), value))\n    throw new ArgumentOutOfRangeException(nameof(value));\n\npreferredFormat = value;","typeGuard":"static bool IsValidInkClipboardFormat(InkCanvasClipboardFormat format)\n    => format == InkCanvasClipboardFormat.Xaml || format == InkCanvasClipboardFormat.Text;","tryCatchPattern":"try\n{\n    processor.PreferredFormat = value;\n}\ncatch (ArgumentException ex)\n{\n    // invalid InkCanvasClipboardFormat value; fall back to Xaml\n    processor.PreferredFormat = InkCanvasClipboardFormat.Xaml;\n}","preventionTips":["Never cast raw ints to InkCanvasClipboardFormat without Enum.IsDefined","Validate persisted settings before applying them as clipboard formats","Remember the enum has exactly two members: Xaml and Text"],"tags":["wpf","ink","clipboard","enum","argument"],"backgroundTag":"invalid-enum-value","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"}