Paste Event in a WPF TextBox

前端 未结 7 792
情深已故
情深已故 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 21:04

    This might not be the exact answer your looking for but here is how to handle pasted text (this also works if user pasted using a the context menu):

    InitializeComponent();
    
                    // "DescriptionTextBox" is a TextBox
                    DataObject.AddPastingHandler(DescriptionTextBox, OnDescriptionPaste);
    
    private void OnDescriptionPaste(object sender, DataObjectPastingEventArgs e)
            {
                if (!e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true))
                    return;
    
                var pastedText = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
                if (string.IsNullOrEmpty(pastedText))
                    return;
    
                var txtBox = (TextBox) sender;
    
                var before = ""; //Text before pasted text
                var after = txtBox.Text; //Text after pasted text
    
                //Get before and after text
                if (txtBox.CaretIndex > 0)
                {
                    before = txtBox.Text.Substring(0, txtBox.CaretIndex);
                    after = txtBox.Text.Substring(txtBox.CaretIndex);
                }
    
                //Do custom logic for handling the pasted text.
                //Split sentences ending with . into new line.
                var parts = pastedText.Split(new []{'.'}, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length > 1)
                {
                    pastedText = parts.Select(x => x.Trim()).ToArray().ToStringX(".\r\n");
                    pastedText += ".";
                }
    
                var newCaretIndex = before.Length + pastedText.Length;
    
                e.CancelCommand(); //Cancels the paste, we do it manually
                txtBox.Text = $"{before}{pastedText}{after}"; //Set new text
                txtBox.CaretIndex = newCaretIndex; //Set new caret index
            }
    

    For handling backspace use PreviewKeyDown event.

提交回复
热议问题