Beispiel #1
0
 /// <summary>
 /// Try to add or remove <paramref name="decoration"/> to the <paramref name="formatting"/> using <paramref name="needToAddDecoration"/>
 /// </summary>
 /// <param name="formatting"></param>
 /// <param name="needToAddDecoration">Determines when the input <paramref name="decoration"/> should be added or removed</param>
 private static TextFormattingRunProperties ApplyDecoration(
     TextFormattingRunProperties formatting, bool needToAddDecoration, TextDecoration decoration)
 {
     if (formatting.TextDecorations.Contains(decoration) ^ needToAddDecoration)
     {
         // NOTE: directly creates a new instance from existing collection to correctly determines
         // in the future that items are contained or not
         var clone = new TextDecorationCollection(formatting.TextDecorations);
         if (needToAddDecoration)
         {
             clone.Add(decoration);
         }
         else
         {
             clone.Remove(decoration);
         }
         return(formatting.SetTextDecorations(clone));
     }
     return(formatting);
 }
        private void UnderlineButton_OnClick(object sender, RoutedEventArgs e)
        {
            bool isChecked = (sender as ToggleButton).IsChecked ?? false;

            if (isChecked)
            {
                RichTextBox.Selection.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Underline);
            }
            else
            {
                TextDecorationCollection textDecorations;
                textDecorations = RichTextBox.Selection.GetPropertyValue(Inline.TextDecorationsProperty) as TextDecorationCollection;

                if (textDecorations.Contains(TextDecorations.Underline[0]))
                {
                    TextDecorationCollection noUnderline = new TextDecorationCollection(textDecorations);
                    noUnderline.Remove(TextDecorations.Underline[0]);  //this is a bool, and could replace Contains above
                    RichTextBox.Selection.ApplyPropertyValue(Inline.TextDecorationsProperty, noUnderline);
                }
            }
        }
Beispiel #3
0
        public static void RemoveTextDecoration(TextDecorationCollection decoration)
        {
            object _decorations = GetRTBValue(TextBlock.TextDecorationsProperty);

            if (_decorations != DependencyProperty.UnsetValue)
            {
                TextDecorationCollection textdecorations = (TextDecorationCollection)_decorations;

                if (textdecorations.IsFrozen)
                {
                    textdecorations = textdecorations.Clone();
                }

                foreach (TextDecoration each in decoration)
                {
                    textdecorations.Remove(each);
                }

                SetRTBValue(TextBlock.TextDecorationsProperty, textdecorations, false);
            }
        }
Beispiel #4
0
 private void ChangeTextStyle(object sender, RoutedEventArgs e)
 {
     if (Selection.Text.Length == 0)
     {
         return;
     }
     if (sender == Bold)
     {
         EditingCommands.ToggleBold.Execute(null, this);
     }
     else if (sender == Italic)
     {
         EditingCommands.ToggleItalic.Execute(null, this);
     }
     else if (sender == Underlined)
     {
         EditingCommands.ToggleUnderline.Execute(null, this);
     }
     else if (sender == Strikethrough)
     {
         TextPointer s               = Selection.Start;
         Span        span            = new Span(Selection.Start, Selection.End);
         bool        isstrikethrough = true;
         foreach (Run item in span.Inlines)
         {
             bool flag = true;
             foreach (TextDecoration dec in item.TextDecorations)
             {
                 if (dec.Location == TextDecorationLocation.Strikethrough)
                 {
                     flag = false;
                 }
             }
             if (flag)
             {
                 isstrikethrough = false; break;
             }
         }
         Inline[] inlines = span.Inlines.ToArray();
         for (int i = 0; i < inlines.Length; i++)
         {
             Run item = inlines[i] as Run;
             TextDecorationCollection collection = item.TextDecorations.Clone();
             if (isstrikethrough)
             {
                 TextDecoration dec = null;
                 foreach (TextDecoration d in collection)
                 {
                     if (d.Location == TextDecorationLocation.Strikethrough)
                     {
                         dec = d;
                     }
                 }
                 if (dec != null)
                 {
                     collection.Remove(dec);
                 }
             }
             else
             {
                 if (!item.TextDecorations.Contains(TextDecorations.Strikethrough[0]))
                 {
                     collection.Add(TextDecorations.Strikethrough);
                 }
             }
             Run r = new Run(item.Text, Selection.Start)
             {
                 TextDecorations = collection, Background = item.Background, FontWeight = item.FontWeight, FontStyle = item.FontStyle, Foreground = item.Foreground
             };
         }
         EditingCommands.Delete.Execute(null, this);
         Selection.Select(s, Selection.Start);
     }
     else
     {
         if (Obfuscated.IsChecked == true)
         {
             Selection.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Black);
         }
         else
         {
             Selection.ApplyPropertyValue(TextElement.BackgroundProperty, null);
         }
     }
 }