0

For a "common" look and feel I have an app that shows the paste/cut/copy buttons in a ribbon but I want to have paste always disabled no matter what field is selected. How do I approach this?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Tony Teveris
  • 165
  • 10

1 Answers1

1

By default all the controls in WPF have a default ContextMenu that allows copy, paste and cut. You can disable this menu by setting this property as “{x:Null}”, but the keys associated with the menu options still work. In order to disable this commands we can use the DataObject class, which have handlers to attach any DepencencyObject in case on Copy or Paste:

DataObject.AddPastingHandler(control, this.OnCancelCommand);
DataObject.AddCopyingHandler(control, this.OnCancelCommand);

Finally in the event handler we need to cancel the current command:

private void OnCancelCommand(object sender, DataObjectEventArgs e)
{
    e.CancelCommand();
}

The CancelCommand method will cancel any ApplicationCommand.Copy, ApplicationCommand.Paste and ApplicationCommand.Cut sent over the control.Now if you want to enable copying than delete the dateobject calling AddCopyingHandler code and it will work than.

NIlesh Sharma
  • 5,445
  • 6
  • 36
  • 53