Hello,
I am trying to give my CodeBlocks inside a RichTextBox.Document a certain style.
Every character inside the CodeBlock should have the same FontFamily and FontSize.
To do this I am going the following way:
1.) Clear all existing styles inside the RichTextBox.CodeFormatter.
2.) Add my own styles.
The code looks like this:
StyleDefinition styleWhite = new StyleDefinition(cl.GetCodeLanguage.Name + "StyleWhite", StyleType.Character);
styleWhite.SpanProperties.ForeColor = Colors.White;
styleWhite.SpanProperties.FontFamily = Wpf.Fonts.Fonts.GetFont(eFont.Consolas);
styleWhite.SpanProperties.FontSize = Unit.PointToDip(fontSize);
StyleDefinition styleKeyWord = new StyleDefinition(cl.GetCodeLanguage.Name + "StyleKeyword", StyleType.Character);
styleKeyWord.SpanProperties.ForeColor = colorKeyWord;
styleKeyWord.SpanProperties.FontFamily = Wpf.Fonts.Fonts.GetFont(eFont.Consolas);
styleKeyWord.SpanProperties.FontSize = Unit.PointToDip(fontSize);
StyleDefinition styleString = new StyleDefinition(cl.GetCodeLanguage.Name + "StyleString", StyleType.Character);
styleString.SpanProperties.ForeColor = colorKeyWord;
styleString.SpanProperties.FontFamily = Wpf.Fonts.Fonts.GetFont(eFont.Consolas);
styleString.SpanProperties.FontSize = Unit.PointToDip(fontSize);
StyleDefinition styleMethod = new StyleDefinition(cl.GetCodeLanguage.Name + "styleMethod", StyleType.Character);
styleMethod.SpanProperties.ForeColor = colorKeyWord;
styleMethod.SpanProperties.FontFamily = Wpf.Fonts.Fonts.GetFont(eFont.Consolas);
styleMethod.SpanProperties.FontSize = Unit.PointToDip(fontSize);
StyleDefinition styleComment = new StyleDefinition(cl.GetCodeLanguage.Name + "StyleComment", StyleType.Character);
styleComment.SpanProperties.ForeColor = colorKeyWord;
styleComment.SpanProperties.FontFamily = Wpf.Fonts.Fonts.GetFont(eFont.Consolas);
styleComment.SpanProperties.FontSize = Unit.PointToDip(fontSize);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Comment);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Keyword);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Method);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.StringLiteral);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Attributes);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.CharacterLiteral);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Constants);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Data);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.ExcludedCode);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Identifier);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.NumberLiteral);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Operator);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.PreprocessorKeyword);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Variable);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.WhiteSpace);
doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Literal);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.Comment, styleComment);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.Keyword, styleKeyWord);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.Method, styleMethod);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.StringLiteral, styleString);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.Attributes, styleWhite);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.CharacterLiteral, styleWhite);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.Constants, styleWhite);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.Data, styleWhite);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.ExcludedCode, styleWhite);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.Identifier, styleWhite);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.NumberLiteral, styleWhite);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.Operator, styleWhite);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.PreprocessorKeyword, styleWhite);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.Variable, styleWhite);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.WhiteSpace, styleWhite);
doc.CodeFormatter.RegisterClassificationType(ClassificationTypes.Literal, styleWhite);
The result of this code is, that I get different FontSizes inside the CodeBlock. See attached *.gif.
So my question is: Which styles do I need to change or add, to give CodeBlocks a consitent look?