Skip to content

[main] Restore WPF clipboard text auto conversion for managed data objects - #12124

Open
Shyam-Gupta wants to merge 4 commits into
mainfrom
dev/shgu/bug11968_main
Open

Shyam-Gupta wants to merge 4 commits into
mainfrom
dev/shgu/bug11968_main

Conversation

@Shyam-Gupta

@Shyam-Gupta Shyam-Gupta commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Fixes #11968

Main PR: this PR targets main.

Release/11.0 companion port: #12125.

Description

Port the changes proposed in #12105 (targeting release/10.0) to main, including both source commits (ff095a98e, 68ce741d9) with cherry-pick provenance. Normalize the added test file's encoding and explicitly implement IDataObjectInternal.SupportsTypedData on DataObject to match the companion's required capability contract. That capability declaration adds no public API.

Restores Windows clipboard text synthesis for WPF after the .NET 10 shared-OLE migration. Opts into a captured native fallback for Text, UnicodeText, and OEMText while preserving managed custom-payload identity, published-format availability, and legacy IDataObject compatibility.

WPF explicitly sets UseNativeClipboardTextFallback => true: fallback is enabled for WPF by this PR. The shared-core policy's default false applies to consumers that do not opt in, not to WPF. WinForms explicitly sets the policy to false.

Plain-text ContainsText/ContainsData checks query Windows' published clipboard formats directly, including native synthesis; later mutations of a managed source do not count as newly published formats. Convenience getters remain usable with legacy-only IDataObject sources. Composed wrappers forward single-argument string getters through the autoConvert: true overloads, matching .NET 9 wrapper dispatch.

Requires the corresponding shared-core WinForms port, dotnet/winforms#15413, and an updated Microsoft.Private.Winforms dependency containing it. The dependency update is not included in this port; integration validation remains required after that package is available.

The companion also supports opt-in fallback in its Framework-targeted shared-core library. This WPF PR targets modern .NET; it does not change the installed .NET Framework WPF product.

Customer Impact

Applications publishing ANSI text can receive false from Clipboard.ContainsText() or empty text from Clipboard.GetText(), breaking workflows that worked in .NET 9.

Regression

Yes. Fixes a .NET 10 regression in clipboard text conversion.

Testing

Local validation below was recorded on 2026-11-12. The WPF build attempt preceded the final explicit-capability declaration; no successful WPF build or test run against the matching new shared-core package is recorded. No tests were rerun for this description-only audit.

  • Ported coverage for text synthesis before/after flushing, disabled managed conversion, published-format availability, typed/legacy retrieval, custom-payload identity, and RTF negative controls.
  • .\build.cmd -projects src\Microsoft.DotNet.Wpf\tests\UnitTests\PresentationCore.Tests\PresentationCore.Tests.csproj -platform x64 -msbuildEngine vs - failed before test execution: native-tool bootstrap failed (Perl, .NET Framework reference assemblies, and Windows SDK redist unavailable); the build also reported missing PresentationBuildTasks references and API compatibility contracts. No WPF test pass is claimed.
  • .\.dotnet\dotnet.exe format whitespace src\Microsoft.DotNet.Wpf\tests\UnitTests\PresentationCore.Tests\PresentationCore.Tests.csproj --no-restore --verify-no-changes --include src\Microsoft.DotNet.Wpf\tests\UnitTests\PresentationCore.Tests\ClipboardTextConversionTests.cs - passed, with workspace-loading warnings.
  • .\.dotnet\dotnet.exe format whitespace src\Microsoft.DotNet.Wpf\tests\UnitTests\PresentationCore.Tests\PresentationCore.Tests.csproj --no-restore --verify-no-changes --include src\Microsoft.DotNet.Wpf\src\PresentationCore\System\Windows\DataObject.cs - passed after the explicit-capability cleanup, with workspace-loading warnings. WPF build/integration validation remains blocked as described above; the build was not retried for this cleanup.
  • Formatting verification across changed test files additionally reports existing indentation at System\Windows\ClipboardTests.cs:118-119, outside the port's changed lines; left unchanged.
  • git diff --check origin/main - passed.
  • Matching WinForms port local validation at 31cf87a1e: 107 modern shared-core, 110 .NET Framework (including 21 fallback cases), and 6 WinForms clipboard regression cases passed; exact commands/results are in [main] Add opt-in native clipboard text fallback for WPF winforms#15413. Of the fallback cases, 17 run on both targets and 4 specifically exercise Framework managed-COM recovery and instance dispatch. Shared-core tests use mock clipboard services; these results do not substitute for WPF integration testing.

Risk

Moderate. WPF explicitly opts in and WinForms explicitly opts out. Native fallback is limited to the three plain-text formats, but composed wrappers also change single-argument string-getter dispatch for other formats. Custom-payload retrieval, enumeration, and writes stay on the managed source. WPF availability checks are case-insensitive; the companion's fallback selection uses exact-case format names. WPF integration has not been validated locally because of the build prerequisites and pending shared-core package update; this is not a standalone fix without that dependency.

Shyam-Gupta and others added 3 commits November 12, 2026 12:06
Preserve Windows plain-text synthesis while retaining in-process custom payloads and legacy IDataObject retrieval. Add clipboard conversion regression coverage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 918733ee-3532-46a0-92bb-728aaa5bb200
(cherry picked from commit ff095a9)
Report wrapped typed-retrieval support and cover native typed text reads plus .NET 9-style getter forwarding.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 918733ee-3532-46a0-92bb-728aaa5bb200
(cherry picked from commit 68ce741)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: e32c8108-e372-40cb-9809-70f4745be462
@Shyam-Gupta
Shyam-Gupta requested a review from a team November 12, 2026 20:20
@Shyam-Gupta
Shyam-Gupta requested a review from a team as a code owner November 12, 2026 20:20
Copilot AI balanced review requested due to automatic review settings November 12, 2026 20:20
@dotnet-policy-service dotnet-policy-service Bot added the PR metadata: Label to tag PRs, to facilitate with triage label Nov 12, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

PresentationCore cannot compile against the pinned Microsoft.Private.Winforms dependency because it lacks the newly used APIs, so the dependency update containing dotnet/winforms#15413 is needed.

1 open issue
What changed in this PR

Ports the clipboard text-conversion fix to main to address #11968, restoring native text synthesis while preserving managed payload identity and legacy IDataObject compatibility.

Changes:

  • Enables native fallback for Text, UnicodeText, and OEMText, with availability based on published clipboard formats.
  • Preserves legacy retrieval through composed data objects.
  • Adds regression coverage for synthesis, flushing, typed retrieval, and custom payloads.
File Description
src/​Microsoft.DotNet.Wpf/​tests/​UnitTests/​PresentationCore.Tests/​System/​Windows/​ClipboardTests.cs Updates text-conversion expectations to require clipboard synthesis.
src/​Microsoft.DotNet.Wpf/​tests/​UnitTests/​PresentationCore.Tests/​ClipboardTextConversionTests.cs Adds synthesis and compatibility regression tests.
src/​Microsoft.DotNet.Wpf/​src/​Shared/​MS/​Win32/​SafeNativeMethodsOther.cs Adds native clipboard-format availability interop.
src/​Microsoft.DotNet.Wpf/​src/​PresentationCore/​System/​Windows/​Ole/​WpfOleServices.cs Enables WPF’s native text fallback.
src/​Microsoft.DotNet.Wpf/​src/​PresentationCore/​System/​Windows/​Ole/​DataObjectAdapter.cs Exposes whether the managed source supports typed retrieval.
src/​Microsoft.DotNet.Wpf/​src/​PresentationCore/​System/​Windows/​dataobject.cs Composes managed data with the captured native fallback.
src/​Microsoft.DotNet.Wpf/​src/​PresentationCore/​System/​Windows/​clipboard.cs Preserves legacy getters and queries native text availability.

Review effort: Balanced

Comment on lines +42 to +43
static DataObject IDataObjectInternal<DataObject, IDataObject>.Create(DataObject clipboardData, IDataObject managedData) =>
new(clipboardData._innerData.WithManagedDataObject(managedData as IDataObjectInternal ?? new DataObjectAdapter(managedData)));
Implement the shared-core capability contract without adding public API, so the property no longer depends on a modern .NET default interface implementation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e32c8108-e372-40cb-9809-70f4745be462
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR metadata: Label to tag PRs, to facilitate with triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression in Clipboard/DataObject text conversion

2 participants