private void cmdBold_Click(object sender, RoutedEventArgs e)
        {
            TextSelection selection = richTextBox.Selection;

            // You could check for no selection, but here the code simply applies the bold formatting
            // to the empty selection (the insertion point), so that when the user starts typing the
            // new text will be bold.

            // GetPropertyValue() returns null if the selection has mixed font bolding.
            // It's up to you what to do in this case, but here the application simply bolds
            // the entire selection.
            FontWeight currentState = FontWeights.Normal;

            if (selection.GetPropertyValue(Run.FontWeightProperty) != DependencyProperty.UnsetValue)
            {
                currentState = (FontWeight)selection.GetPropertyValue(Run.FontWeightProperty);
            }

            if (currentState == FontWeights.Normal)
            {
                selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold);
            }
            else
            {
                selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal);
            }

            // A nice detail is to bring the focus back to the text box, so the user can resume typing.
            richTextBox.Focus();
        }
Exemple #2
0
        private void ItalicButton_Click(object sender, RoutedEventArgs e)
        {
            TextSelection text = this.WhitodoText.Selection;

            if (!text.IsEmpty)
            {
                bool isItalic = false;
                try
                {
                    FontStyle fs = (FontStyle)text.GetPropertyValue(System.Windows.Controls.RichTextBox.FontStyleProperty);
                    if (fs == FontStyles.Italic)
                    {
                        isItalic = true;
                    }
                }
                catch (Exception) { }
                if (isItalic)
                {
                    text.ApplyPropertyValue(System.Windows.Controls.RichTextBox.FontStyleProperty, FontStyles.Normal);
                    this.ItalicButton.Background = Brushes.White;
                }
                else
                {
                    text.ApplyPropertyValue(System.Windows.Controls.RichTextBox.FontStyleProperty, FontStyles.Italic);
                    this.ItalicButton.Background = Brushes.LightPink;
                }
            }
            this.WhitodoText.Focus();
        }
Exemple #3
0
        private void UnderLineButton_Click(object sender, RoutedEventArgs e)
        {
            TextSelection text = this.WhitodoText.Selection;

            if (!text.IsEmpty)
            {
                bool isUnderline = false;
                try
                {
                    TextDecorationCollection ul = (TextDecorationCollection)text.GetPropertyValue(TextBlock.TextDecorationsProperty);
                    if (ul == TextDecorations.Underline)
                    {
                        isUnderline = true;
                    }
                }
                catch (Exception) { }
                if (isUnderline)
                {
                    text.ApplyPropertyValue(TextBlock.TextDecorationsProperty, null);
                    this.UnderLineButton.Background = Brushes.White;
                }
                else
                {
                    text.ApplyPropertyValue(TextBlock.TextDecorationsProperty, TextDecorations.Underline);
                    this.UnderLineButton.Background = Brushes.LightPink;
                }
            }
            this.WhitodoText.Focus();
        }
        public static double?GetFontSize(this TextSelection selection)
        {
            double?nullValue = null;
            var    property  = selection.GetPropertyValue(TextElement.FontSizeProperty);

            return(property is double?PixelsToPoints((double)property) : nullValue);
        }
        private void RichTxtBoxSelectionChanged(object sender, RoutedEventArgs e)
        {
            sel = page1.Selection;

            obj = sel.GetPropertyValue(Inline.FontSizeProperty);
            cmbFontSize.Text = obj.ToString();

            obj            = sel.GetPropertyValue(Inline.FontWeightProperty);
            bold.IsChecked = (obj != DependencyProperty.UnsetValue) && (obj.Equals(FontWeights.Bold));

            obj = sel.GetPropertyValue(Inline.FontStyleProperty);
            italic.IsChecked = (obj != DependencyProperty.UnsetValue) && (obj.Equals(FontStyles.Italic));

            obj = sel.GetPropertyValue(Inline.TextDecorationsProperty);
            underline.IsChecked = (obj != DependencyProperty.UnsetValue) && (obj.Equals(TextDecorations.Underline));
        }
Exemple #6
0
        public void GetPropertyValue()
        {
            TextSelection ts = rtb.Selection;

            Assert.Throws <NullReferenceException> (delegate {
                ts.GetPropertyValue(null);
            }, "null");
        }
Exemple #7
0
        private void rTextBox_SelectionChanged(object sender, RoutedEventArgs e)
        {
            TextSelection selection = rTextBox.Selection;

            ToggleBoldTB.IsChecked      = (FontWeight)selection.GetPropertyValue(TextElement.FontWeightProperty) == FontWeights.Bold;
            ToggleItalicTB.IsChecked    = (FontStyle)selection.GetPropertyValue(TextElement.FontStyleProperty) == FontStyles.Italic;
            ToggleUnderlineTB.IsChecked = (TextDecorationCollection)selection.GetPropertyValue(Inline.TextDecorationsProperty) == TextDecorations.Underline;

            FontFamilyCB.SelectedItem     = selection.GetPropertyValue(TextElement.FontFamilyProperty);
            FontSizeCB.SelectedItem       = selection.GetPropertyValue(TextElement.FontSizeProperty);
            FontColorPicker.SelectedColor = (selection.GetPropertyValue(TextElement.ForegroundProperty) as SolidColorBrush).Color;

            Paragraph start = selection.Start.Paragraph;

            if (start != null && start.Parent is ListItem)
            {
                TextMarkerStyle markerStyle = ((ListItem)start.Parent).List.MarkerStyle;
                ToggleBulletsTB.IsChecked   = markerStyle is >= TextMarkerStyle.Disc and <= TextMarkerStyle.Box;
                ToggleNumberingTB.IsChecked = markerStyle is >= TextMarkerStyle.LowerRoman and <= TextMarkerStyle.Decimal;
            }
            else
            {
                ToggleBulletsTB.IsChecked   = false;
                ToggleNumberingTB.IsChecked = false;
            }

            var    lineSpacingVal = selection.GetPropertyValue(Block.LineHeightProperty);
            double lineSpacing    = lineSpacingVal is Double ? (double)lineSpacingVal : Double.NaN;

            LineSpacingCB.Text = Double.IsNaN(lineSpacing) ? "-" : lineSpacing.ToString();
        }
Exemple #8
0
        private static void Synchronize <T>(TextSelection selection, DependencyProperty property, Action <T> methodToCall)
        {
            object value = selection.GetPropertyValue(property);

            if (value != DependencyProperty.UnsetValue)
            {
                methodToCall((T)value);
            }
        }
Exemple #9
0
        private void WhitodoText_SelectionChanged(object sender, RoutedEventArgs e)
        {
            TextSelection text = this.WhitodoText.Selection;

            if (!text.IsEmpty)
            {
                bool isBold      = false;
                bool isItalic    = false;
                bool isUnderline = false;
                try
                {
                    TextDecorationCollection ul = (TextDecorationCollection)text.GetPropertyValue(TextBlock.TextDecorationsProperty);
                    if (ul == TextDecorations.Underline)
                    {
                        isUnderline = true;
                    }
                }
                catch (Exception) { }
                try
                {
                    FontWeight fw = (FontWeight)text.GetPropertyValue(System.Windows.Controls.RichTextBox.FontWeightProperty);
                    if (fw == FontWeights.Bold)
                    {
                        isBold = true;
                    }
                }
                catch (Exception) { }
                try
                {
                    FontStyle fs = (FontStyle)text.GetPropertyValue(System.Windows.Controls.RichTextBox.FontStyleProperty);
                    if (fs == FontStyles.Italic)
                    {
                        isItalic = true;
                    }
                }
                catch (Exception) { }
                this.BoldButton.Background      = isBold ? Brushes.LightPink : Brushes.White;
                this.ItalicButton.Background    = isItalic ? Brushes.LightPink : Brushes.White;
                this.UnderLineButton.Background = isUnderline ? Brushes.LightPink : Brushes.White;
            }
            this.WhitodoText.Focus();
        }
Exemple #10
0
        public static FontFamily GetFontFamily(this TextSelection selection)
        {
            object ffObj = selection.GetPropertyValue(TextElement.FontFamilyProperty);

            if (ffObj == DependencyProperty.UnsetValue)
            {
                return(null);
            }
            else
            {
                return((FontFamily)ffObj);
            }
        }
Exemple #11
0
        public static Brush GetForeground(this TextSelection selection)
        {
            object brushObj = selection.GetPropertyValue(TextElement.ForegroundProperty);

            if (brushObj == DependencyProperty.UnsetValue)
            {
                return(null);
            }
            else
            {
                return((Brush)brushObj);
            }
        }
Exemple #12
0
        public static double GetFontSize(this TextSelection selection)
        {
            object fsObj = selection.GetPropertyValue(TextElement.FontSizeProperty);

            if (fsObj == DependencyProperty.UnsetValue)
            {
                return(double.NaN);
            }
            else
            {
                return((double)fsObj);
            }
        }
Exemple #13
0
        private void button_B_Click(object sender, RoutedEventArgs e)
        {
            string        value       = (string)((Button)e.OriginalSource).Content;
            TextSelection selection   = MyRichTextBox.Selection;
            FontWeight    weightState = FontWeights.Normal;

            if (value == "B")
            {
                if (selection.GetPropertyValue(Run.FontWeightProperty) != DependencyProperty.UnsetValue)
                {
                    weightState = (FontWeight)selection.GetPropertyValue(Run.FontWeightProperty);
                }
                if (weightState == FontWeights.Normal)
                {
                    selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold);
                }
                else
                {
                    selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal);
                }
            }
        }
Exemple #14
0
        private void button_I_Click(object sender, RoutedEventArgs e)
        {
            string        value      = (string)((Button)e.OriginalSource).Content;
            TextSelection selection  = MyRichTextBox.Selection;
            FontStyle     styleState = FontStyles.Normal;

            if (value == "I")
            {
                if (selection.GetPropertyValue(Run.FontStyleProperty) != DependencyProperty.UnsetValue)
                {
                    styleState = (FontStyle)selection.GetPropertyValue(Run.FontStyleProperty);
                }
                if (styleState == FontStyles.Italic)
                {
                    selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Normal);
                }
                else
                {
                    selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Italic);
                }
            }
        }
Exemple #15
0
        private void Synchronize <T>(
            TextSelection selectedText,
            DependencyProperty property,
            Action <T> actionToPerform
            )
        {
            object propertyValue = selectedText.GetPropertyValue(property);

            if (propertyValue != DependencyProperty.UnsetValue)
            {
                actionToPerform((T)propertyValue);
            }
        }
        private void cmdItalic_Click(object sender, RoutedEventArgs e)
        {
            TextSelection selection = richTextBox.Selection;

            FontStyle currentState = FontStyles.Normal;

            if (selection.GetPropertyValue(Run.FontStyleProperty) != DependencyProperty.UnsetValue)
            {
                currentState = (FontStyle)selection.GetPropertyValue(Run.FontStyleProperty);
            }

            if (currentState == FontStyles.Italic)
            {
                selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Normal);
            }
            else
            {
                selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Italic);
            }

            richTextBox.Focus();
        }
        private void cmdUnderline_Click(object sender, RoutedEventArgs e)
        {
            TextSelection selection = richTextBox.Selection;

            TextDecorationCollection currentState = null;

            if (selection.GetPropertyValue(Run.TextDecorationsProperty) != DependencyProperty.UnsetValue)
            {
                currentState = (TextDecorationCollection)selection.GetPropertyValue(Run.TextDecorationsProperty);
            }

            if (currentState != TextDecorations.Underline)
            {
                selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
            }
            else
            {
                selection.ApplyPropertyValue(Run.TextDecorationsProperty, null);
            }

            richTextBox.Focus();
        }
        private void boldFontButton_Click(object sender, RoutedEventArgs e)
        {
            TextSelection selectedText = textBox.Selection;

            if (selectedText.GetPropertyValue(TextElement.FontWeightProperty) == (object)FontWeights.Bold)
            {
                selectedText.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);
            }
            else
            {
                selectedText.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
            }
            textBox.Focus();
        }
Exemple #19
0
        private void button_U_Click(object sender, RoutedEventArgs e)
        {
            string                   value        = (string)((Button)e.OriginalSource).Content;
            TextSelection            selection    = MyRichTextBox.Selection;
            TextDecorationCollection currentState = null;

            if (value == "U")
            {
                if (selection.GetPropertyValue(Run.TextDecorationsProperty) != DependencyProperty.UnsetValue)
                {
                    currentState = (TextDecorationCollection)selection.GetPropertyValue(Run.TextDecorationsProperty);
                }

                if (currentState != TextDecorations.Underline)
                {
                    selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
                }
                else
                {
                    selection.ApplyPropertyValue(Run.TextDecorationsProperty, null);
                }
            }
        }
Exemple #20
0
        public static bool IsBold(this TextSelection selection)
        {
            object fwObj = selection.GetPropertyValue(TextElement.FontWeightProperty);

            if (fwObj == DependencyProperty.UnsetValue)
            {
                return(false);
            }
            else
            {
                var fontWeight = (FontWeight)fwObj;
                return(fontWeight == FontWeights.Bold);
            }
        }
Exemple #21
0
        public static bool IsItalic(this TextSelection selection)
        {
            object fsObj = selection.GetPropertyValue(TextElement.FontStyleProperty);

            if (fsObj == DependencyProperty.UnsetValue)
            {
                return(false);
            }
            else
            {
                var fontStyle = (FontStyle)fsObj;
                return(fontStyle == FontStyles.Italic);
            }
        }
        private void cursiveFontButton_Click(object sender, RoutedEventArgs e)
        {
            TextSelection selectedText = textBox.Selection;

            if ((FontStyle)selectedText.GetPropertyValue(TextElement.FontStyleProperty) == FontStyles.Italic)
            {
                selectedText.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Normal);
            }
            else
            {
                selectedText.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic);
            }

            textBox.Focus();
        }
        private void underlineFontButton_Click(object sender, RoutedEventArgs e)
        {
            TextSelection selectedText = textBox.Selection;

            if (selectedText.GetPropertyValue(TextElement.FontStyleProperty) == (object)FontStyles.Oblique)
            {
                selectedText.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Normal);
            }
            else
            {
                selectedText.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Oblique);
            }

            textBox.Focus();
        }
Exemple #24
0
        private void UnderlineBnt_Unselected(object sender, RoutedEventArgs e)
        {
            if (TextZone == null)
            {
                return;
            }
            TextSelection ts = TextZone.Selection;

            if (ts != null)
            {
                (ts.GetPropertyValue(Inline.TextDecorationsProperty) as TextDecorationCollection).TryRemove(TextDecorations.Underline, out TextDecorationCollection textDecorations);
                ts.ApplyPropertyValue(Inline.TextDecorationsProperty, textDecorations);
            }
            TextZone.Focus();
        }
        void Selection_Changed(object sender, EventArgs e)
        {
            TextSelection richTextBox = sender as TextSelection;
            Brush         b           = richTextBox.GetPropertyValue(Hyperlink.ForegroundProperty) as Brush;

            if (b != null)
            {
                SolidColorBrush solidcolor = (SolidColorBrush)b;
                Forcground = solidcolor;
            }
            //if (richTextBox.Tag != null)
            //{
            //    ToolboxItem item = richTextBox.Tag as ToolboxItem;
            //    item.ItemForeground = solidcolor;
            //}
        }
        internal static Object GetSelectionPropertyValue(TextSelection selection, DependencyProperty formattingProperty)
        {
            // This is necessary due to the emergence of unexpected errors
            // when pasting text from other editors, such as a Microsoft Word

            Object propertyValue;

            try
            {
                propertyValue = selection.GetPropertyValue(formattingProperty);
            }
            catch (Exception) // including NullReferenceException if selection is null
            {
                propertyValue = null;
            }

            return(propertyValue);
        }
Exemple #27
0
        private object UpdateToolbarSelection(object args)
        {
            try
            {
                TextSelection selText = this._TextBox.Selection;

                object value;
                //Get the style for the current selection.
                //  - Font size.
                value = selText.GetPropertyValue(TextElement.FontSizeProperty);
                this._toolSize.Text = (value == DependencyProperty.UnsetValue) ? "" : value.ToString();
                // - Font typeface.
                value = selText.GetPropertyValue(TextElement.FontFamilyProperty);
                if (value != DependencyProperty.UnsetValue)
                {
                    if (this._toolFont.Items.Contains((FontFamily)value))
                    {
                        this._toolFont.SelectedIndex = this._toolFont.Items.IndexOf(value);
                    }
                    else
                    {
                        this._toolFont.Text = ((FontFamily)value).Source;
                    }
                }
                else
                {
                    this._toolFont.Text = "";
                }
                // - Font weight. (Bold)
                value = selText.GetPropertyValue(TextElement.FontWeightProperty);
                this._toolBold.IsChecked = (value == DependencyProperty.UnsetValue) ? false : ((FontWeight)value != FontWeights.Normal);
                // - Font Style. (Italic)
                value = selText.GetPropertyValue(TextElement.FontStyleProperty);
                this._toolItalic.IsChecked = (value == DependencyProperty.UnsetValue) ? false : ((FontStyle)value != FontStyles.Normal);
                // - Font underline.
                value = selText.GetPropertyValue(Inline.TextDecorationsProperty);
                bool isUnder = false;
                if (value != DependencyProperty.UnsetValue)
                {
                    foreach (TextDecoration text in (TextDecorationCollection)value)
                    {
                        if (text.Location == TextDecorationLocation.Underline)
                        {
                            isUnder = true;
                        }
                    }
                }
                this._toolUnderline.IsChecked = isUnder;
                // - Font alignment
                value = selText.GetPropertyValue(Paragraph.TextAlignmentProperty);
                this._toolJusLeft.IsChecked   = (value == DependencyProperty.UnsetValue) ? true : (TextAlignment)value == TextAlignment.Left;
                this._toolJusCenter.IsChecked = (value == DependencyProperty.UnsetValue) ? false : (TextAlignment)value == TextAlignment.Center;
                this._toolJusRight.IsChecked  = (value == DependencyProperty.UnsetValue) ? false : (TextAlignment)value == TextAlignment.Right;
                // - Marker # Numbering
                Paragraph startParagraph = selText.Start.Paragraph;
                Paragraph endParagraph   = selText.End.Paragraph;
                if (startParagraph != null && endParagraph != null &&
                    (startParagraph.Parent is ListItem) && (endParagraph.Parent is ListItem) &&
                    ((ListItem)startParagraph.Parent).List == ((ListItem)endParagraph.Parent).List)
                {
                    TextMarkerStyle markerStyle = ((ListItem)startParagraph.Parent).List.MarkerStyle;
                    this._toolBullet.IsChecked = (markerStyle == TextMarkerStyle.Disc ||
                                                  markerStyle == TextMarkerStyle.Circle ||
                                                  markerStyle == TextMarkerStyle.Square ||
                                                  markerStyle == TextMarkerStyle.Box);
                    this._toolNumber.IsChecked = (markerStyle == TextMarkerStyle.LowerRoman ||
                                                  markerStyle == TextMarkerStyle.UpperRoman ||
                                                  markerStyle == TextMarkerStyle.LowerLatin ||
                                                  markerStyle == TextMarkerStyle.UpperLatin ||
                                                  markerStyle == TextMarkerStyle.Decimal);
                }
                else
                {
                    this._toolBullet.IsChecked = false;
                    this._toolNumber.IsChecked = false;
                }
            }
            finally
            {
                this.updSelectionProcessing = false;
            }
            return(null);
        }
Exemple #28
0
 private static FontStyle GetFontStyle(TextSelection sel)
 {
     return(sel.GetPropertyValue(TextBlock.FontStyleProperty).CastOrDefault(FontStyles.Normal));
 }
Exemple #29
0
 private static TextAlignment GetTextAlignment(TextSelection sel)
 {
     return(sel.GetPropertyValue(Block.TextAlignmentProperty).CastOrDefault(TextAlignment.Left));
 }
Exemple #30
0
 private static TextDecorationCollection GetTextDecorations(TextSelection sel)
 {
     return(sel.GetPropertyValue(Paragraph.TextDecorationsProperty).CastOrDefault <TextDecorationCollection>(null));
 }