{"record":{"id":"082638e444cd4b31","repo":"dotnet/wpf","slug":"sr-dataobject-dataobjectmusthaveatleastoneformat","errorCode":null,"errorMessage":"SR.DataObject_DataObjectMustHaveAtLeastOneFormat","messagePattern":"SR\\.DataObject_DataObjectMustHaveAtLeastOneFormat","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DataObjectPastingEventArgs.cs","lineNumber":128,"sourceCode":"        /// with current DataObject (but not necessarily with SourceDataObject).\n        /// </summary>\n        public IDataObject DataObject\n        {\n            get \n            { \n                return _dataObject; \n            }\n\n            set\n            {\n                string[] availableFormats;\n\n                ArgumentNullException.ThrowIfNull(value);\n\n                availableFormats = value.GetFormats(autoConvert: false);\n                if (availableFormats == null || availableFormats.Length == 0)\n                {\n                    throw new ArgumentException(SR.DataObject_DataObjectMustHaveAtLeastOneFormat);\n                }\n\n                _dataObject = value;\n                _formatToApply = availableFormats[0];\n            }\n        }\n\n        /// <summary>\n        /// String identifying a format an editor has choosen\n        /// as a candidate for applying in Paste operation.\n        /// An application can change this choice after inspecting\n        /// the content of data object.\n        /// The value assigned to FormatToApply must be present\n        /// on a current DataObject. The invariant is\n        /// this.DataObject.GetDataPresent(this.FormatToApply) === true.\n        /// </summary>\n        public string FormatToApply\n        {","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DataObjectPastingEventArgs.cs#L110-L146","documentation":"The DataObjectPastingEventArgs.DataObject property setter throws ArgumentException when the assigned IDataObject exposes no formats at all (GetFormats returns null or empty). A paste operation is meaningless without any data format to apply, so the setter refuses data objects with zero content.","triggerScenarios":"Assigning an empty DataObject to DataObjectPastingEventArgs.DataObject, e.g. args.DataObject = new DataObject(); inside a Pasting handler that intends to replace the paste payload.","commonSituations":"Replacing the clipboard payload in a Pasting handler but constructing a fresh DataObject before adding any data; a pipeline step that cleared all formats; passing a custom IDataObject whose GetFormats implementation is unimplemented/returns empty.","solutions":["Add at least one data entry before assigning: obj.SetData(DataFormats.Text, text)","Assign an IDataObject whose GetFormats() returns a non-empty array","If the replacement data is unavailable, cancel the paste via CancelCommand() instead of assigning an empty object"],"exampleFix":"// before\nargs.DataObject = new DataObject();\n// after\nvar obj = new DataObject();\nobj.SetData(DataFormats.Text, replacementText);\nargs.DataObject = obj;","handlingStrategy":"validation","validationCode":"var formats = replacementData?.GetFormats();\nif (formats == null || formats.Length == 0) throw new InvalidOperationException(\"Replacement data object must contain at least one format.\");","typeGuard":"bool HasAnyFormat(IDataObject data) { var f = data.GetFormats(); return f != null && f.Length > 0; }","tryCatchPattern":"try { args.DataObject = replacementData; }\ncatch (ArgumentException) { args.CancelCommand(); /* abort paste instead */ }","preventionTips":["Always SetData at least one entry before assigning a replacement DataObject","If replacement data is unavailable, use CancelCommand rather than an empty object","Test custom IDataObject implementations to ensure GetFormats is implemented"],"tags":["wpf","argumentexception","clipboard","empty-data"],"backgroundTag":"empty-required-field","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"}