2016-09-09 5 views
0

J'essaie de créer un éditeur de texte en tant que projet d'apprentissage WPF/C#.
J'ai 3 boutons à bascule (souligné, souligné et barré).
- elles peuvent apparaître en combinaison, mais comment les détecter?Détection de plusieurs TextDecorations dans la sélection RichTectBox

Mon selectionChanged eventhandler:

private void rt_SelectionChanged(object sender, RoutedEventArgs e) 
    { 
     bool IsTextUnderline = false; 
     bool IsTextStrikethrough = false; 
     bool IsOverline = false; 

     TextRange range = new TextRange(rt.Selection.Start, rt.Selection.End); 

     var decor = range.GetPropertyValue(Inline.TextDecorationsProperty); 

     if (decor != DependencyProperty.UnsetValue) 
      { 
      TextDecorationCollection coll = (TextDecorationCollection)decor; 

      IsTextStrikethrough = (coll.Contains(TextDecorations.Strikethrough)); 

Mais .Contains() attend un TextDecorationCollection comme paramètre ...
- tout à fait déroutant

Répondre

0

J'étais proche (juste raté [0] - quoi que signifie), voici celui qui fonctionne:

private void rt_SelectionChanged(object sender, RoutedEventArgs e) 
{ 
    Object temp = rt.Selection.GetPropertyValue(Inline.TextDecorationsProperty); 
    if (temp.ToString() != "{DependencyProperty.UnsetValue}") // multivalue, can't handle..? 
    { 
     TextDecorationCollection decs = (TextDecorationCollection)temp; 
     btnUnderl.IsChecked = decs.Contains(TextDecorations.Underline[0]); 
     btnStrike.IsChecked = decs.Contains(TextDecorations.Strikethrough[0]); 
     btnBaseli.IsChecked = decs.Contains(TextDecorations.Baseline[0]); 
     btnOverli.IsChecked = decs.Contains(TextDecorations.OverLine[0]); 
    } 
}