dotnet/wpf · error · ArgumentException

SR.DataObject_DataObjectMustHaveAtLeastOneFormat

Error message

SR.DataObject_DataObjectMustHaveAtLeastOneFormat

What it means

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.

Solutions

  1. Add at least one data entry before assigning: obj.SetData(DataFormats.Text, text)
  2. Assign an IDataObject whose GetFormats() returns a non-empty array
  3. If the replacement data is unavailable, cancel the paste via CancelCommand() instead of assigning an empty object

Example fix

// before
args.DataObject = new DataObject();
// after
var obj = new DataObject();
obj.SetData(DataFormats.Text, replacementText);
args.DataObject = obj;
Defensive patterns

Strategy: validation

Validate before calling

var formats = replacementData?.GetFormats();
if (formats == null || formats.Length == 0) throw new InvalidOperationException("Replacement data object must contain at least one format.");

Type guard

bool HasAnyFormat(IDataObject data) { var f = data.GetFormats(); return f != null && f.Length > 0; }

Try / catch

try { args.DataObject = replacementData; }
catch (ArgumentException) { args.CancelCommand(); /* abort paste instead */ }

Prevention

When it happens

Trigger: Assigning an empty DataObject to DataObjectPastingEventArgs.DataObject, e.g. args.DataObject = new DataObject(); inside a Pasting handler that intends to replace the paste payload.

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

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DataObjectPastingEventArgs.cs:128

        /// with current DataObject (but not necessarily with SourceDataObject).
        /// </summary>
        public IDataObject DataObject
        {
            get 
            { 
                return _dataObject; 
            }

            set
            {
                string[] availableFormats;

                ArgumentNullException.ThrowIfNull(value);

                availableFormats = value.GetFormats(autoConvert: false);
                if (availableFormats == null || availableFormats.Length == 0)
                {
                    throw new ArgumentException(SR.DataObject_DataObjectMustHaveAtLeastOneFormat);
                }

                _dataObject = value;
                _formatToApply = availableFormats[0];
            }
        }

        /// <summary>
        /// String identifying a format an editor has choosen
        /// as a candidate for applying in Paste operation.
        /// An application can change this choice after inspecting
        /// the content of data object.
        /// The value assigned to FormatToApply must be present
        /// on a current DataObject. The invariant is
        /// this.DataObject.GetDataPresent(this.FormatToApply) === true.
        /// </summary>
        public string FormatToApply
        {

View on GitHub (pinned to 81131a70a4)