Paste Event in a WPF TextBox

前端 未结 7 781
情深已故
情深已故 2020-11-30 20:10

I have created a custom control inheriting TextBox. This custom control is a numeric TextBox, only supporting numbers.

I am using OnP

7条回答
  •  温柔的废话
    2020-11-30 20:54

    Here's some code I had lying around in case I ever needed it. Might help you.

    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;
        ...
    }
    

提交回复
热议问题