From 8052b16ca185c5ece32e3880ca4ccc2c07bdce8f Mon Sep 17 00:00:00 2001 From: uye <99072975+ABA2396@users.noreply.github.com> Date: Tue, 15 Jul 2025 16:55:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20ComboBox=20=E5=89=AA=E8=B4=B4=E6=9D=BF?= =?UTF-8?q?=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UIBehaviors/ClipboardInterceptor.cs | 35 +++++++++++++------ src/MaaWpfGui/Res/Styles/ComboBox.xaml | 4 ++- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/MaaWpfGui/Extensions/UIBehaviors/ClipboardInterceptor.cs b/src/MaaWpfGui/Extensions/UIBehaviors/ClipboardInterceptor.cs index 7db8a3b6ee..e6febf5459 100644 --- a/src/MaaWpfGui/Extensions/UIBehaviors/ClipboardInterceptor.cs +++ b/src/MaaWpfGui/Extensions/UIBehaviors/ClipboardInterceptor.cs @@ -26,7 +26,7 @@ namespace MaaWpfGui.Extensions.UIBehaviors "EnableSafeClipboard", typeof(bool), typeof(ClipboardInterceptor), - new PropertyMetadata(false, OnEnableSafeClipboardChanged)); + new(false, OnEnableSafeClipboardChanged)); public static void SetEnableSafeClipboard(DependencyObject element, bool value) => element.SetValue(EnableSafeClipboardProperty, value); @@ -47,26 +47,42 @@ namespace MaaWpfGui.Extensions.UIBehaviors case DataGrid dg when (bool)e.NewValue: AddCommandBindingsToDataGrid(dg); break; + case ComboBox cb when (bool)e.NewValue && cb.IsEditable: + cb.Loaded += OnComboBoxLoaded; + break; } } private static void AddCommandBindingsToTextBox(TextBox tb) { - tb.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, OnCopyTextBox)); - tb.CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut, OnCutTextBox)); - tb.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, OnPasteTextBox)); + tb.CommandBindings.Add(new(ApplicationCommands.Copy, OnCopyTextBox)); + tb.CommandBindings.Add(new(ApplicationCommands.Cut, OnCutTextBox)); + tb.CommandBindings.Add(new(ApplicationCommands.Paste, OnPasteTextBox)); } private static void AddCommandBindingsToRichTextBox(RichTextBox rtb) { - rtb.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, OnCopyRichTextBox)); - rtb.CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut, OnCutRichTextBox)); - rtb.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, OnPasteRichTextBox)); + rtb.CommandBindings.Add(new(ApplicationCommands.Copy, OnCopyRichTextBox)); + rtb.CommandBindings.Add(new(ApplicationCommands.Cut, OnCutRichTextBox)); + rtb.CommandBindings.Add(new(ApplicationCommands.Paste, OnPasteRichTextBox)); } private static void AddCommandBindingsToDataGrid(DataGrid dg) { - dg.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, OnCopyDataGrid)); + dg.CommandBindings.Add(new(ApplicationCommands.Copy, OnCopyDataGrid)); + } + + private static void OnComboBoxLoaded(object sender, RoutedEventArgs e) + { + if (sender is not ComboBox cb) + { + return; + } + + if (cb.Template.FindName("PART_EditableTextBox", cb) is TextBox textBox) + { + AddCommandBindingsToTextBox(textBox); + } } private static void OnCopyTextBox(object sender, ExecutedRoutedEventArgs e) @@ -213,7 +229,7 @@ namespace MaaWpfGui.Extensions.UIBehaviors if (sender is not DataGrid dg) { return; - }; + } // 获取选中单元格内容,拼成制表符分隔的文本 var selectedCells = dg.SelectedCells; @@ -253,6 +269,5 @@ namespace MaaWpfGui.Extensions.UIBehaviors e.Handled = true; } - } } diff --git a/src/MaaWpfGui/Res/Styles/ComboBox.xaml b/src/MaaWpfGui/Res/Styles/ComboBox.xaml index 8bfa77a474..a50d3d187e 100644 --- a/src/MaaWpfGui/Res/Styles/ComboBox.xaml +++ b/src/MaaWpfGui/Res/Styles/ComboBox.xaml @@ -1,10 +1,12 @@ - +