Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void checkToolBarButtons()
- {
- // this method checks if the button should be highlighted or not.
- Func<Button, object> check = (Button button) =>
- {
- try
- {
- // the button has two costum properties that allow to determine which text property should be
- // applied to the selected text.
- string propString = ToolBarButton.GetDocumentProperty(button);
- // e.g. "TextDecorations"
- DependencyProperty dependency = this.getPropertyByString(propString);
- // e.g. TextDecorationsProperty
- string propertyValue = ToolBarButton.GetDocumentPropertyValue(button);
- // e.g. "Underline"
- if (dependency != null)
- {
- TextRange selectionRange = new TextRange(this.richTextBox.Selection.Start, this.richTextBox.Selection.End);
- object selectedProperty = selectionRange.GetPropertyValue(dependency);
- if (selectedProperty.GetType() == typeof(TextDecorationCollection) && ((TextDecorationCollection)selectedProperty).Count > 0)
- {
- if (selectedProperty.Equals(TextDecorations.Underline))
- {
- // this code is never reached
- selectedProperty = "Underline";
- }
- else if (selectedProperty.Equals(TextDecorations.Strikethrough))
- {
- // this code is never reached
- selectedProperty = "Strikethrough";
- }
- }
- if (selectedProperty.ToString() == propertyValue)
- {
- button.Background = new SolidColorBrush(Colors.Yellow);
- }
- else
- {
- button.Background = new SolidColorBrush(Colors.LightGray);
- }
- }
- }
- catch (Exception ex)
- {
- }
- return null;
- };
- foreach (FrameworkElement ctrl in toolBar.Children)
- {
- if (ctrl.GetType() == typeof(StackPanel))
- {
- foreach (Button button in ((Panel)ctrl).Children)
- {
- check(button);
- }
- }
- else if (ctrl.GetType() == typeof(Button))
- {
- check((Button)ctrl);
- }
- }
- }
- /// <summary>
- /// converts a string to a DependencyProperty
- /// </summary>
- /// <param name="propertyString"></param>
- /// <returns></returns>
- private DependencyProperty getPropertyByString(string propertyString)
- {
- switch (propertyString)
- {
- case ("FontStyleProperty"): return FontStyleProperty;
- case ("FontWeightProperty"): return FontWeightProperty;
- case ("TextDecorations"): return TextBlock.TextDecorationsProperty;
- case ("TextAlignment"): return TextBlock.TextAlignmentProperty;
- default:
- break;
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement