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 DependencyProperty CopyCommandProperty =
DependencyProperty.Register("CopyCommand",
typeof(RoutedCommand), typeof(MultiFormatDataView));
/// <summary>
/// .NET Property for <see cref="DataSourceProperty"/>.
/// </summary>
@ -119,35 +115,43 @@ public class MultiFormatDataView : Control
remove { this.RemoveHandler(ClearRequestedEvent, value); }
}
private static RoutedCommand? copyCommand;
/// <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>
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}");

Loading…
Cancel
Save