All I am really trying to do is create a textBox where non-contiguous segments of text can be highlighted at any given time.
From the experimenting I have done, it would appear that the System.Windows.Controls.RichTextBox is a good fit for this when customized slightly.

Extending RichTextBox and getting multiple pieces of text highlighted doesn't seem to be a problem (I think), but I am unable to actually get the custom wpf control in the application.
From what I have read, the Custom WPF control template is missing from the express edition, but I have read that other people have had success in making their own template... unfortunately they don't explain how they did it.

In other words, If all I am trying to do is extend RichtTextBox with a couple of extra features, how do I get VS to accept that custom control?

Dani AI

Generated

Good start by — subclassing RichTextBox is the right path when you want new behavior without inventing a whole new look. A few focused tips that help the control show reliably in VS2010 Express and make non‑contiguous highlighting practical.

Make the control designer-friendly: the class must be public, have a parameterless constructor, and not throw in construction. Guard runtime-only init so the designer can instantiate the type without hitting runtime-only code:

public class CustomRichTextBox : RichTextBox
{
    public CustomRichTextBox()
    {
        if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            return;

        // runtime initialization here
    }
}

Two practical ways to implement multiple highlights (tradeoffs included):

  • Apply formatting to discrete TextRanges. This is simple, edits move the formatting with the text, and it serializes with the FlowDocument. Example to apply a background to a range:
    var range = new TextRange(startPointer, endPointer);
    range.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Yellow);
  • Draw transient highlights with an Adorner. This leaves the document DOM untouched (good for ephemeral selections) but requires mapping TextPointers to screen rects and handling layout/scroll changes.

Persistence and management: if you use formatting, saving the FlowDocument with a XamlPackage preserves highlights; you can later reopen the package and the background formatting returns. To track and remove specific highlights, either wrap segments in Span/Inline with a Tag you control, or keep a small metadata list and search the document for matching formatting or tagged Inlines.

Toolbox/designer issues: build the project after adding the control, check the Output window for design-time exceptions (those commonly hide the control from the designer), and if the control lives in another assembly add a project reference and use the clr-namespace;assembly mapping in XAML. ’s pointer to templates is useful only if you need a lookless control — for behavior-only changes you don’t need a full control template.

Recommended Answers

All 2 Replies

This is a little tutorial:

You will find more if you google.

For anyone else who is just getting into WPF and doesn't quite understand how the xaml relates to the code and VS, here is what I did to get my custom WPF richTextBox as an addable control in the VS toolbox.

Create a WPF Application by starting a new project and selecting 'WPF Application'. Once the project is created and you are dumped into the editor, double click MainWindow.xaml and add the following line:

xmlns:local="clr-namespace:WpfApplication1"

You will have to change 'WpfApplication1' to the actual namespace of your project. Also, that line will have to go after the 'Window x:Class' line and before the 'Title' line, just like in this example:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns=""
        xmlns:x=""
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="554" Width="525">

From what I can tell, adding that line will allow you to reference any custom controls you have in your project, by using the word 'local' (I think 'local' can be changed to a different word if desired).

Now you should be able to drag and drop your custom control from the VS toolbox onto the application's main window, or whatever control you want. If your custom control doesn't appear in the toolbox, try compiling.
Otherwise, if it still doesn't appear you can just add a reference to it via the xaml. Like so:

<local:CustomRichTextBox Margin="12,92,12,12" x:Name="customRichTextBox1" VerticalScrollBarVisibility="Auto" />

Notice how the word 'local' is being used again, and notice how after the colon is the actual name of my class which is extending a WPF RichTextBox, in other words, 'CustomRichTextBox' is my custom control. Otherwise, the other properties ('Margin' 'x:Name' etc...) May not be required or may be different for your control. And as you would hope adjusting the control in the visual editor will automatically write and set most of those values for you.


Anyway, I went over this stuff since I spent a few hours last night trying to find a concise explanation of just how to get VS to recognize my custom control and allow me to use it. Many of the tutorials I found focused on other aspects of custom controls, and although they featured some of the same lines I mentioned in this post, they did not communicate the importance of those lines--or at least I didn't understand it.
Otherwise, if anyone else notices anything incorrect about what I have suggested, or would like to add an enlightening pointer, go right ahead. Because I barely know what I am talking about.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.