Textbox Overriding OnRender and FormattedText problem [modified]
-
I created a custom textbox inheriting from wpf textbox and overriding OnRender. I used FormattedText to set background colors of few words in the textbox. Somehow the fonts are becoming bold, not sure why. How to get-rid of that? Here is my code.
if (this.Text != "") { FormattedText formattedText = new FormattedText( this.Text, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily,this.FontStyle,this.FontWeight,this.FontStretch), this.FontSize, this.Foreground); //Text that matches the textbox's double leftMargin = 4.0 + this.BorderThickness.Left; double topMargin = 2 + this.BorderThickness.Top; formattedText.MaxTextWidth = this.ViewportWidth; // space for scrollbar formattedText.MaxTextHeight = Math.Max(this.ActualHeight + this.VerticalOffset, 0); //Adjust for scrolling drawingContext.PushClip(new RectangleGeometry(new Rect(0, 0, this.ActualWidth, this.ActualHeight)));//restrict text to textbox //Background hilight if (HighlightText != null && HighlightText.Length > 0) { foreach (string text in HighlightText) { int txtEnd = this.Text.Length; int index = 0; int lastIndex = this.Text.LastIndexOf(text, StringComparison.OrdinalIgnoreCase); while (index <= lastIndex) { index = this.Text.IndexOf(text, index, StringComparison.OrdinalIgnoreCase); Geometry geom = formattedText.BuildHighlightGeometry(new Point(leftMargin, topMargin - this.VerticalOffset), index, text.Length); if (geom != null) { drawingContext.DrawGeometry(Brushes.Yellow, null, geom); } index += 1; } } } drawingContext.DrawText(formattedText, new Point(leftMargin, topMargin - this.VerticalOffset)); }
Thanks.modified on Tuesday, July 28, 2009 5:35 PM
-
I created a custom textbox inheriting from wpf textbox and overriding OnRender. I used FormattedText to set background colors of few words in the textbox. Somehow the fonts are becoming bold, not sure why. How to get-rid of that? Here is my code.
if (this.Text != "") { FormattedText formattedText = new FormattedText( this.Text, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily,this.FontStyle,this.FontWeight,this.FontStretch), this.FontSize, this.Foreground); //Text that matches the textbox's double leftMargin = 4.0 + this.BorderThickness.Left; double topMargin = 2 + this.BorderThickness.Top; formattedText.MaxTextWidth = this.ViewportWidth; // space for scrollbar formattedText.MaxTextHeight = Math.Max(this.ActualHeight + this.VerticalOffset, 0); //Adjust for scrolling drawingContext.PushClip(new RectangleGeometry(new Rect(0, 0, this.ActualWidth, this.ActualHeight)));//restrict text to textbox //Background hilight if (HighlightText != null && HighlightText.Length > 0) { foreach (string text in HighlightText) { int txtEnd = this.Text.Length; int index = 0; int lastIndex = this.Text.LastIndexOf(text, StringComparison.OrdinalIgnoreCase); while (index <= lastIndex) { index = this.Text.IndexOf(text, index, StringComparison.OrdinalIgnoreCase); Geometry geom = formattedText.BuildHighlightGeometry(new Point(leftMargin, topMargin - this.VerticalOffset), index, text.Length); if (geom != null) { drawingContext.DrawGeometry(Brushes.Yellow, null, geom); } index += 1; } } } drawingContext.DrawText(formattedText, new Point(leftMargin, topMargin - this.VerticalOffset)); }
Thanks.modified on Tuesday, July 28, 2009 5:35 PM
i found the mistake i was doing. Do not draw the text, which will draw on top the existing text of the textbox making it look bold. I removed
drawingContext.DrawText(formattedText, new Point(leftMargin, topMargin - this.VerticalOffset));
from the logic and it worked fine.