fixed copy command. was not working on all instances of MultiFormatDataView. with adapted binding it works.

master
Jonas Arnold 3 years ago
parent 39cd61307e
commit 3632fca13c
  1. 49
      MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs

@ -58,10 +58,6 @@ public class MultiFormatDataView : Control
public static readonly RoutedEvent ClearRequestedEvent; public static readonly RoutedEvent ClearRequestedEvent;
public static readonly DependencyProperty CopyCommandProperty =
DependencyProperty.Register("CopyCommand",
typeof(RoutedCommand), typeof(MultiFormatDataView));
/// <summary> /// <summary>
/// .NET Property for <see cref="DataSourceProperty"/>. /// .NET Property for <see cref="DataSourceProperty"/>.
/// </summary> /// </summary>
@ -119,35 +115,43 @@ public class MultiFormatDataView : Control
remove { this.RemoveHandler(ClearRequestedEvent, value); } remove { this.RemoveHandler(ClearRequestedEvent, value); }
} }
private static RoutedCommand? copyCommand;
/// <summary> /// <summary>
/// .NET Property for <see cref="CopyCommandProperty"/> /// Binding that binds the <see cref="copyCommand"/> to the internal method <see cref="CopyCommand_Executed(object, ExecutedRoutedEventArgs)"/>.
/// </summary>
private static CommandBinding copyCommandBinding;
/// <summary>
/// Routed command that is called from the template.
/// </summary> /// </summary>
private static RoutedCommand? copyCommand;
public static RoutedCommand? CopyCommand public static RoutedCommand? CopyCommand
{ {
get { return copyCommand; } get { return copyCommand; }
} }
#endregion #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 // register copy command
copyCommand = new RoutedCommand("CopyCommand", typeof(MultiFormatDataView)); copyCommand = new RoutedCommand("CopyCommand", typeof(MultiFormatDataView));
var binding = new CommandBinding copyCommandBinding = new CommandBinding
{ {
Command = copyCommand Command = copyCommand
}; };
binding.Executed += CopyCommand_Executed; ; copyCommandBinding.Executed += CopyCommand_Executed; ;
CommandBindings.Add(binding);
} }
static MultiFormatDataView() public MultiFormatDataView()
{ {
DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiFormatDataView), new FrameworkPropertyMetadata(typeof(MultiFormatDataView))); // add copy command binding on each instance
CommandBindings.Add(copyCommandBinding);
ClearRequestedEvent = EventManager.RegisterRoutedEvent("ClearRequested",
RoutingStrategy.Bubble, typeof(RoutedEventArgs),
typeof(MultiFormatDataView));
} }
public override void OnApplyTemplate() public override void OnApplyTemplate()
@ -254,18 +258,23 @@ public class MultiFormatDataView : Control
RaiseEvent(args); 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()) switch (e.Parameter.ToString())
{ {
case "Character": case "Character":
Clipboard.SetText(this.DataSource.GetSelectedDataAsString(Core.Types.FormatType.Character)); Clipboard.SetText(mfdv.DataSource.GetSelectedDataAsString(Core.Types.FormatType.Character));
break; break;
case "Hexadecimal": case "Hexadecimal":
Clipboard.SetText(this.DataSource.GetSelectedDataAsString(Core.Types.FormatType.Hexadecimal)); Clipboard.SetText(mfdv.DataSource.GetSelectedDataAsString(Core.Types.FormatType.Hexadecimal));
break; break;
case "Binary": case "Binary":
Clipboard.SetText(this.DataSource.GetSelectedDataAsString(Core.Types.FormatType.Binary)); Clipboard.SetText(mfdv.DataSource.GetSelectedDataAsString(Core.Types.FormatType.Binary));
break; break;
default: default:
throw new ArgumentException($"'{CopyCommand_Executed}()' does not have handling implemented for CommandParameter={e.Parameter}"); throw new ArgumentException($"'{CopyCommand_Executed}()' does not have handling implemented for CommandParameter={e.Parameter}");

Loading…
Cancel
Save