Souligner ou pas du texte wpf

moi411 Messages postés 179 Date d'inscription samedi 22 novembre 2003 Statut Membre Dernière intervention 25 juin 2017 - 19 juin 2011 à 09:53
moi411 Messages postés 179 Date d'inscription samedi 22 novembre 2003 Statut Membre Dernière intervention 25 juin 2017 - 20 juin 2011 à 19:00
Bonjour,
Comme dit dans le titre j'ai besoin, dans application WPF, de souligner du texte ou d'annuler le soulignement.

Pour souligner ça va, j'utilise ces 2 lignes (rtb est ma richTextBox):

TextRange tr = new TextRange(rtb.Selection.Start, rtb.Selection.End);
tr.ApplyPropertyValue(Inline.TextDecorationsProperty,
TextDecorations.Underline);

Par contre mon problème est pour arrêter de souligner.
Quand on appuie sur un bouton on souligne et quand on appuie à nouveau sur ce bouton on arrête de souligner...

Voilà, j'espère que quelqu'un pourra m'aider!
A bientôt.
moi

4 réponses

moi411 Messages postés 179 Date d'inscription samedi 22 novembre 2003 Statut Membre Dernière intervention 25 juin 2017 3
19 juin 2011 à 12:49
Autre question, Qu'est-ce que j'ai mal fait?!?
Que le texte soit souligné ou pas, je rentre dans le cas du "else" (texte non souligné)...
Plus bas le bout de code devant faire le test.

TextRange tr = new TextRange(rtb.Selection.Start, rtb.Selection.End);

if (tr.GetPropertyValue(Inline.TextDecorationsProperty) == TextDecorations.Underline) { MessageBox.Show("true"); }
else { MessageBox.Show("false"); }


Merci d'avance... encore!
moi
0
moi411 Messages postés 179 Date d'inscription samedi 22 novembre 2003 Statut Membre Dernière intervention 25 juin 2017 3
19 juin 2011 à 13:44
Pour la première question j'ai trouvé:

TextRange tr = new TextRange(rtb.Selection.Start, rtb.Selection.End);

TextDecorationCollection tdc = (TextDecorationCollection)rtb.Selection.GetPropertyValue(Inline.TextDecorationsProperty);

if (souligne.IsChecked == true)
{
tdc = TextDecorations.Underline;
}
else
{
tdc = new TextDecorationCollection();
}

tr.ApplyPropertyValue(Inline.TextDecorationsProperty, tdc);

Ca c'est pour la première question...
Mais la deuxième, à savoir pourquoi mon texte (souligné ou pas) est toujours considéré comme étant non souligné, je n'ai toujours pas trouvé.
Merci à celui qui m'aidera à trouver une solution!!!


moi
0
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
20 juin 2011 à 14:59
Hello,

pour la seconde question :

if (tr.GetPropertyValue(Inline.TextDecorationsProperty) | TextDecorations.Underline == TextDecorations.Underline) { 
   MessageBox.Show("true"); 
} else { 
   MessageBox.Show("false"); 
} 


Utilisation du type Enum


Sébastien FERRAND
Ingénieur Concepteur Senior
Microsoft Visual C# MVP 2004 - 2009
Blog Photo
0
moi411 Messages postés 179 Date d'inscription samedi 22 novembre 2003 Statut Membre Dernière intervention 25 juin 2017 3
20 juin 2011 à 19:00
Salut,
Tout d'abord merci de m'accorder du temps!

Ensuite, c'est pareil... Tant que le fichier reste ouvert ça marche; mais quand je le ferme (après enregistrement) et que je le rouvre, il ne reconnait à nouveau plus ce que j'ai souligné...
(La mise en forme grasse, ou italique ne pose aucun problème mais dès que je souligne ça déconne)

En attendant une réponse, je te souhaite une bonne soirée.
moi
0
Rejoignez-nous