Utilisation de RichTextBox pour coloration syntaxique
Bonjour,
je tente d'utiliser une RichTextBox (WPF) pour colorer du texte, seulement ça marche pas des masses.
Le contrôle étant super complexe de base déjà, je suis dans l'impossibilité de savoir pourquoi le code suivant ne fonctionne pas :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| TextRange range = new TextRange(MyTextInput.Document.ContentStart, MyTextInput.Document.ContentEnd);
range.Text = @"TOP a
multiline text or file END";
Regex reg = new Regex("(top|file|end)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (Match match in reg.Matches(range.Text))
{
TextPointer start = range.Start.GetPositionAtOffset(match.Index, LogicalDirection.Forward);
TextPointer end = range.Start.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward);
// text contient bien le texte que je souhaite colorer
string text = range.Text.Substring(match.Index, match.Length);
// Mais ici ...
TextRange textrange = new TextRange(start, end);
textrange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
textrange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
} |
En fait, seul TOP est bien coloré, file & end ne le sont pas alors que je boucle bien dessus. Je ne comprend pas d'où peux provenir le problème, si vous avez des idées ...