David Ching
David Ching

Reputation: 1963

Call default WPF Textbox Paste handler from my command binding

I have declared a WPF Textbox with a command binding for Paste:

<TextBox x:Name="txtMsg">
    <TextBox.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Paste" Executed="TxtMsg_OnPaste"/>
    </TextBox.CommandBindings>
</TextBox>

My code-behind is

private void TxtMsg_OnPaste(object sender, ExecutedRoutedEventArgs e)
{
    if (Clipboard.ContainsText())
    {
        // Let the textbox paste text its normal way
        // This code is called, but the textbox doesn't paste anything!
        e.Handled = false;
    }
    else
    {
        // My logic for other clipboard formats
        // ...
        e.Handled = true;
    }
}

The problem is setting e.Handled = false does not cause the textbox to paste anything. Nothing is pasted when the clipboard contains text!

How do I get the WPF Textbox to handle the Paste command normally in this case?

Upvotes: 1

Views: 528

Answers (1)

SledgeHammer
SledgeHammer

Reputation: 7680

Call the Paste() method on the TextBox. Or you can just get the text and set it yourself -- same difference.

Upvotes: 1

Related Questions