Comment puis-je détecter le formatage du texte actuel à la position du curseur dans un fichier RichTextBox WPF?Obtenir FontWeight/FontStyle/TextDecorations à partir de WPF RichTextBox
3
A
Répondre
0
Essayez le code ci-dessous où RTB est RichTextBox:
TextRange tr = new TextRange(rtb.Selection.Start, rtb.Selection.End);
object oFont = tr.GetPropertyValue(Run.FontFamilyProperty);
3
j'utiliser le CaretPosition au lieu du début de la sélection et à la fin, comme si le RichTextBox a fait une sélection qui couvre plusieurs domaines de vous formatage obtiendrait DependencyProperty.UnsetValue.
TextRange tr = new TextRange(rtb.CaretPosition, rtb.CaretPosition); object oFont = tr.GetPropertyValue(Run.FontFamilyProperty);
3
L'auteur de ce fil a également posé des questions sur TextDecorations où vous n'avez pas fourni des exemples de code et son différent à utiliser. Je posterai cela comme une autre solution :
var obj = _myText.GetPropertyValue(Inline.TextDecorationsProperty);
if (obj == DependencyProperty.UnsetValue)
IsTextUnderline = false;// mixed formatting
if (obj is TextDecorationCollection)
{
var objProper = obj as TextDecorationCollection;
if (objProper.Count > 0)
IsTextUnderline = true; // all underlined
else
IsTextUnderline = false; // nothing underlined
}
1
Voici une solution qui détermine FontWeight, FontStyle, TextDecorations (biffés, souligné) et Super- et Subscripts.
TextRange textRange = new TextRange(rtb.Selection.Start, rtb.Selection.End);
bool IsTextUnderline = false;
bool IsTextStrikethrough = false;
bool IsTextBold = false;
bool IsTextItalic = false;
bool IsSuperscript = false;
bool IsSubscript = false;
// determine underline property
if (textRange.GetPropertyValue(Inline.TextDecorationsProperty).Equals(TextDecorations.Strikethrough))
IsTextStrikethrough = true; // all underlined
else if (textRange.GetPropertyValue(Inline.TextDecorationsProperty).Equals(TextDecorations.Underline))
IsTextUnderline = true; // all strikethrough
// determine bold property
if (textRange.GetPropertyValue(Inline.FontWeightProperty).Equals(FontWeights.Bold))
IsTextBold = true; // all bold
// determine if superscript or subscript
if (textRange.GetPropertyValue(Inline.BaselineAlignmentProperty).Equals(BaselineAlignment.Subscript))
IsSubscript = true; // all subscript
else if (textRange.GetPropertyValue(Inline.BaselineAlignmentProperty).Equals(BaselineAlignment.Superscript))
IsSuperscript = true; // all superscript
Questions connexes
- 1. WPF Richtextbox FontFace/FontSize
- 2. WPF richTextBox question
- 3. Lecture à partir de RichTextBox dans VC++
- 4. WPF Aucun événement de contrôles dans RichTextBox
- 5. Importation de XAML dans WPF RichTextBox
- 6. C#/WPF: Désactiver Text-Wrap de RichTextBox
- 7. Obtenir un élément WPF ListBox à partir de MouseLeftButtonDown
- 8. Comment obtenir le RTF de RichTextBox
- 9. Insérer une image dans WPF RichTextBox
- 10. WPF: façon rapide d'appliquer le formatage à RichTextBox
- 11. Comment WPF RIchtextbox peut-il afficher Unicode?
- 12. WPF RichTextBox ne supprime pas le texte
- 13. Réglage culture/langue dans RichTextBox WPF
- 14. Set texte RTF en WPF RichTextBox contrôle
- 15. Remplacer le texte dans WPF Richtextbox
- 16. Obtenir un contrôle spécifique à partir d'une fenêtre WPF?
- 17. Obtenir un bitmap à partir d'une fenêtre d'application WPF?
- 18. Modification de l'incrément de police pour WPF RichTextBox
- 19. WPF RichTextBox sélection de l'onglet manger de la mémoire système!
- 20. Comment trouver la zone d'affichage d'un RichTextBox WPF?
- 21. Comment obtenir un texte de paragraphe unique à partir d'une zone richtext en double-cliquant dessus
- 22. RichTextBox
- 23. Impression/Aperçu de WPT RichTextBox - ApplicationCommands.PrintPreview?
- 24. WPF - Faites glisser à partir de DataTemplate
- 25. Cliquez sur HyperLinks dans un RichTextBox sans maintenir CTRL - WPF
- 26. WPF RichTextBox - La méthode ScrollToEnd() n'entraîne aucun défilement
- 27. WPF RichTextBox - Remplacer le texte sélectionné par un contrôle personnalisé
- 28. Détermination du InlineUIContainer sélectionné dans un fichier WPF RichTextBox
- 29. Comment forcer le mode insertion dans un fichier WPF RichTextBox
- 30. Comment ignorer l'orthographe lors d'une exécution dans WPF RichTextBox
le plus difficile n'a pas été répondu voir ma réponse! – msfanboy