From 3632fca13ca31a434ec8afb861ec0c5a3d60b282 Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Tue, 6 Jun 2023 14:24:47 +0200 Subject: [PATCH] fixed copy command. was not working on all instances of MultiFormatDataView. with adapted binding it works. --- .../MultiFormatDataView.cs | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs b/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs index 57542c5..daae6fb 100644 --- a/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs +++ b/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs @@ -58,10 +58,6 @@ public class MultiFormatDataView : Control public static readonly RoutedEvent ClearRequestedEvent; - public static readonly DependencyProperty CopyCommandProperty = - DependencyProperty.Register("CopyCommand", - typeof(RoutedCommand), typeof(MultiFormatDataView)); - /// /// .NET Property for . /// @@ -119,35 +115,43 @@ public class MultiFormatDataView : Control remove { this.RemoveHandler(ClearRequestedEvent, value); } } - private static RoutedCommand? copyCommand; /// - /// .NET Property for + /// Binding that binds the to the internal method . + /// + private static CommandBinding copyCommandBinding; + /// + /// Routed command that is called from the template. /// + private static RoutedCommand? copyCommand; public static RoutedCommand? CopyCommand { get { return copyCommand; } } + #endregion - public MultiFormatDataView() + static MultiFormatDataView() { + DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiFormatDataView), new FrameworkPropertyMetadata(typeof(MultiFormatDataView))); + + // register clear event + ClearRequestedEvent = EventManager.RegisterRoutedEvent("ClearRequested", + RoutingStrategy.Bubble, typeof(RoutedEventArgs), + typeof(MultiFormatDataView)); + // register copy command copyCommand = new RoutedCommand("CopyCommand", typeof(MultiFormatDataView)); - var binding = new CommandBinding + copyCommandBinding = new CommandBinding { Command = copyCommand }; - binding.Executed += CopyCommand_Executed; ; - CommandBindings.Add(binding); + copyCommandBinding.Executed += CopyCommand_Executed; ; } - static MultiFormatDataView() + public MultiFormatDataView() { - DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiFormatDataView), new FrameworkPropertyMetadata(typeof(MultiFormatDataView))); - - ClearRequestedEvent = EventManager.RegisterRoutedEvent("ClearRequested", - RoutingStrategy.Bubble, typeof(RoutedEventArgs), - typeof(MultiFormatDataView)); + // add copy command binding on each instance + CommandBindings.Add(copyCommandBinding); } public override void OnApplyTemplate() @@ -254,18 +258,23 @@ public class MultiFormatDataView : Control RaiseEvent(args); } - private void CopyCommand_Executed(object sender, ExecutedRoutedEventArgs e) + private static void CopyCommand_Executed(object sender, ExecutedRoutedEventArgs e) { + // guard null and check if source is correct + if(e.Source == null || e.Source is not MultiFormatDataView mfdv) + { return; } + + // copy to clipboard according to type switch (e.Parameter.ToString()) { case "Character": - Clipboard.SetText(this.DataSource.GetSelectedDataAsString(Core.Types.FormatType.Character)); + Clipboard.SetText(mfdv.DataSource.GetSelectedDataAsString(Core.Types.FormatType.Character)); break; case "Hexadecimal": - Clipboard.SetText(this.DataSource.GetSelectedDataAsString(Core.Types.FormatType.Hexadecimal)); + Clipboard.SetText(mfdv.DataSource.GetSelectedDataAsString(Core.Types.FormatType.Hexadecimal)); break; case "Binary": - Clipboard.SetText(this.DataSource.GetSelectedDataAsString(Core.Types.FormatType.Binary)); + Clipboard.SetText(mfdv.DataSource.GetSelectedDataAsString(Core.Types.FormatType.Binary)); break; default: throw new ArgumentException($"'{CopyCommand_Executed}()' does not have handling implemented for CommandParameter={e.Parameter}");