问题
Sometimes a user will type a website URL while chatting in a Chat Application. I need to make the RichTextBox detect these URLs automatically, but I do not know how. General TextBox does not detect URLs, so i have used RichTextBox instead. I know RichTextBoxes can detect URLs but I don't know how. Can anybody suggest how I might do this?
回答1:
As schoola pointed out, the WPF RichTextBox does not auto-detect URLs. However this article describes a fairly simple (150 line) implementation of a custom RichTextBox that can be used for this very purpose.
回答2:
The WPF RichTextBox does not support auto-detection of URLs, but the Windows Forms Rich Text Box does. You might use a WindowsFormsHost element in your WPF application like this:
[...]
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
[...]
<WindowsFormsHost >
<wf:RichTextBox DetectUrls="True" />
</WindowsFormsHost>
回答3:
Have a look at these articles -
Auto-detecting Hyperlinks in RichTextBox - Part I
Auto-detecting Hyperlinks in RichTextBox - Part II
来源:https://stackoverflow.com/questions/9124429/how-to-detect-urls-written-or-pasted-in-a-richtextbox-in-wpf-application