0

I want to add an event that is triggered when I try to paste content (Ctrl + V) inside my application in general.

I saw this, but it only applies to textboxes:

public Window1()
{
    InitializeComponent();

    // "tb" is a TextBox
    DataObject.AddPastingHandler(tb, OnPaste);
}

private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
    var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
    if (!isText) return;

    var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
    ...
}
SideSky
  • 313
  • 3
  • 15
  • 1
    Do you mean Ctrl + V on buttons e.g.? Posted link is good and has a lot of answers. – Rekshino May 18 '22 at 07:18
  • My goal is that an event is triggered when, for example, a label or the window is focused and CTRL + V is pressed. In the linked post, this only works when a text box is focused. – SideSky May 18 '22 at 07:21
  • 1
    You may want to use `ApplicationCommands.Paste`, e.g. like here: https://stackoverflow.com/a/61342127/1136211 – Clemens May 18 '22 at 08:59

0 Answers0