I am trying to disable Copy/Paste/Cut feature in special window in xaml(WPF)
.
I set the PreviewKeyDown="Window_PreviewKeyDown"
in properties of special window. This way any key down it will track and cancel. Below code is working fine for disallow copy & paste for outside.
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if(Keyboard.Modifiers == ModifierKeys.Control && (e.Key == Key.C || e.Key == Key.X))
{
e.Handled = true;
}
if(Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.V)
{
e.Handled = true;
}
}
This works
Now, I would like to allow copying and pasting from within the application itself.
Is there a way to disallow outside copy pasting into an application except for what has been copied and pasted from with an application?
Here is the answer. We need to clear the clipboard when deactivate the window and can be set the text again into clipboard while activate.
private string oldClipboardContent { get; set; } = "";
private void Window_Activated(object sender, EventArgs e)
{
Clipboard.SetText(oldClipboardContent);
}
private void Window_Deactivated(object sender, EventArgs e)
{
oldClipboardContent = Clipboard.GetText();
Clipboard.Clear();
}