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?

Recommended Answers

All 2 Replies

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:


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"
        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, learning, and sharing knowledge.