RichTextBox Example

richtextbox.jpg

The RichTextBox is fairly straight-forward to implement. TextBox is NOT an IParent but it is an IScrollViewerStrategy; consequently, you may call ScrollViewerStyle.ApplyToScrollViewer to apply styling/colors to the scrollbar as long as you call InitializeForScrollViewerSupport first.

RichTextBox rtb = new RichTextBox();
rtb.InitializeForScrollViewerSupport(500D, 150D, 140D);  //
ScrollViewerStyle.ApplyToScrollViewer<RichTextBox>(rtb, "MistyRose", "White", "Red");
rtb.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;

You may use a FlowDocument with Paragraphs and assign it to the RichTextBox's Document property in order to add text. For example:

FlowDocument flowDoc = new FlowDocument();
rtb.Document = flowDoc
          .AddParagraph("Arial", FontStyles.Normal, 12, Brushes.Black, FontWeights.Bold, new Run("Hello World\n\n\n\n"))
          .AddParagraph("Arial", FontStyles.Normal, 12, Brushes.Red, FontWeights.Normal, "Here is longer text to force the vertical scrollbar to display.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nMake this a long run-on sentence just to make the scrollbar display.");

To add icons or buttons with common styling features for manipulating text within the RichTextBox takes a little more work, but should be possible. Here are examples for adding buttons to Bold Selected Text and to Apply Verdana Font to selected text.

Button b99 = new Button();
b99.Initialize(155D, 27D, ContainerType.ContentControlPanel);
b99.BeginComposite<Button>().AddText<Button>(0,0,  "Bold Selected Text").EndComposite<Button, ButtonArgs>(null);
b99.SubscribeEventOnParent<Button, MouseButtonEventArgs>("PreviewMouseDown", (ve => { rtb.Selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold); }));

Button b100 = new Button();
b100.Initialize(305D, 27D, ContainerType.ContentControlPanel);
b100.BeginComposite<Button>().AddText<Button>(0, 0, "Apply Verdana to Selected Text").EndComposite<Button, ButtonArgs>(null);
b100.SubscribeEventOnParent<Button, MouseButtonEventArgs>("PreviewMouseDown", (ve => { rtb.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty, new FontFamily("Verdana")); })); 

Last edited May 29, 2014 at 6:25 AM by stagathome0069, version 8

Comments

No comments yet.