Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>WPF's TextBox and RichTextBox classes do not directly support multiselection, but as with most parts of WPF it's extremely easy to customize its existing RichTextBox to get this ability.</p> <p>The steps are:</p> <ul> <li>Create a class deriving from RichTextBox</li> <li>Add an "AdditionalRanges" property of type <code>ObservableCollection&lt;TextRange&gt;</code> which will contain all selected ranges except the current TextSelection</li> <li>Override <code>OnPreviewMouseLeftButtonDown</code>: If Ctrl is pressed, combine the current TextSelection into your "AdditionalRanges" property and clear Selection, otherwise clear "AdditionalRanges".</li> <li>In the constructor, add a <code>CollectionChanged</code> handler to "AdditionalRanges" that uses <code>TextRange.ApplyPropertyValue()</code> to make added ranges in the collection appear hilighted and removed ranges appear normally.</li> </ul> <p>In your implementation I also recommend you implement a few more properties for convenience:</p> <ul> <li>An "AllRanges" property that combines the TextSelection with AdditionalRanges</li> <li>A bindable "Text" property</li> <li>A bindable "SelectedText" property</li> </ul> <p>These are all quite trivial to implement.</p> <p>Final notes:</p> <ul> <li>When updating AdditionalRanges or computing AllRanges, if the TextSelection overlaps an existing AdditionalRange, replace it with a combined range otherwise add the TextSelection to the list.</li> <li>You can add a <code>TextChanged</code> handler to know when to update the "Text" property, and a PropertyChangedCallback to know when to update the FlowDocument</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload