How can I accept pastes from the clipboard in my UserControl?

I have a UserControl that, oddly enough, links many other controls and logic to a neat little package. It has a Text property that accepts stringand does magic, displaying results for the user. Tall.

I get this text from a TextBox. The user pastes the text from the clipboard into the text field that is bound to the DP on my UserControl.

What I would like to do is cut out the middle person and accept the paste in my UserControl.

I have already tried using the attached DataObject.Pasting event , but it does not seem to work.

How do you do this?


My own question answered with my current solution, but honestly, it "smells". If someone has a better answer, add it, and if it works, and better, I will choose it.

+3
source share
1 answer

My brain fired. Team bindings . Now I know when someone is trying to insert and can take it from there.

XAML:

<UserControl.CommandBindings>
    <CommandBinding
        Command="Paste"
        Executed="CommandBinding_Executed"/>
</UserControl.CommandBindings>

(sorry bad code trying to get this to work for now) And the event handler:

try
{
    var text = Clipboard.GetData(DataFormats.Text) as string;
    if (string.IsNullOrWhiteSpace(text))
        return;
    Lines = new Lines(text);
    e.Handled = true;
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Paste failed", MessageBoxButton.OK);
}

It smells, IMHO. But I'm not sure how to handle this.

+2
source

Source: https://habr.com/ru/post/1759580/


All Articles