Skip to content

Commit 7600316

Browse files
authored
Merge pull request w-ahmad#305 from mavaddat/clipboard_issue
Clipboard issue
2 parents 57d927c + 3e5c7d3 commit 7600316

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

src/TableView.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Collections.Generic;
99
using System.ComponentModel;
1010
using System.ComponentModel.DataAnnotations;
11+
using System.Diagnostics;
1112
using System.IO;
1213
using System.Linq;
1314
using System.Reflection;
@@ -447,17 +448,44 @@ private TableViewCellSlot GetNextSlot(TableViewCellSlot? currentSlot, bool isShi
447448
/// </summary>
448449
internal void CopyToClipboardInternal(bool includeHeaders)
449450
{
451+
// Skip TableView copy logic when a cell editor already handles Ctrl+C.
452+
// TextBox, PasswordBox, and RichEditBox all implement their own copy behavior.
453+
var focused = FocusManager.GetFocusedElement() as FrameworkElement;
454+
if (focused is TextBox || focused is PasswordBox || focused is RichEditBox)
455+
{
456+
return;
457+
}
458+
450459
var args = new TableViewCopyToClipboardEventArgs(includeHeaders);
451460
OnCopyToClipboard(args);
452461

453462
if (args.Handled)
454463
{
455464
return;
456465
}
466+
467+
var content = GetSelectedClipboardContent(includeHeaders);
457468

458-
var package = new DataPackage();
459-
package.SetText(GetSelectedClipboardContent(includeHeaders));
460-
Clipboard.SetContent(package);
469+
if (string.IsNullOrWhiteSpace(content))
470+
{
471+
return;
472+
}
473+
474+
// Try/catch to prevent CLIPBRD_E_CANT_OPEN crashes.
475+
try
476+
{
477+
var package = new DataPackage();
478+
package.SetText(content);
479+
480+
Clipboard.SetContent(package);
481+
}
482+
catch (Exception ex)
483+
{
484+
// Clipboard failures are normal on Windows (e.g., CLIPBRD_E_CANT_OPEN).
485+
// Swallow to avoid crashing the application.
486+
Debug.WriteLine(
487+
$"TableView: Clipboard.SetContent failed: {ex}");
488+
}
461489
}
462490

463491
/// <summary>

0 commit comments

Comments
 (0)