Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. private void checkToolBarButtons()
  2. {
  3. // this method checks if the button should be highlighted or not.
  4. Func<Button, object> check = (Button button) =>
  5. {
  6. try
  7. {
  8. // the button has two costum properties that allow to determine which text property should be
  9. // applied to the selected text.
  10. string propString = ToolBarButton.GetDocumentProperty(button);
  11. // e.g. "TextDecorations"
  12. DependencyProperty dependency = this.getPropertyByString(propString);
  13. // e.g. TextDecorationsProperty
  14. string propertyValue = ToolBarButton.GetDocumentPropertyValue(button);
  15. // e.g. "Underline"
  16.  
  17. if (dependency != null)
  18. {
  19. TextRange selectionRange = new TextRange(this.richTextBox.Selection.Start, this.richTextBox.Selection.End);
  20. object selectedProperty = selectionRange.GetPropertyValue(dependency);
  21.  
  22. if (selectedProperty.GetType() == typeof(TextDecorationCollection) && ((TextDecorationCollection)selectedProperty).Count > 0)
  23. {
  24. if (selectedProperty.Equals(TextDecorations.Underline))
  25. {
  26. // this code is never reached
  27. selectedProperty = "Underline";
  28. }
  29. else if (selectedProperty.Equals(TextDecorations.Strikethrough))
  30. {
  31. // this code is never reached
  32. selectedProperty = "Strikethrough";
  33. }
  34. }
  35.  
  36. if (selectedProperty.ToString() == propertyValue)
  37. {
  38. button.Background = new SolidColorBrush(Colors.Yellow);
  39. }
  40. else
  41. {
  42. button.Background = new SolidColorBrush(Colors.LightGray);
  43. }
  44. }
  45. }
  46. catch (Exception ex)
  47. {
  48.  
  49. }
  50. return null;
  51. };
  52.  
  53. foreach (FrameworkElement ctrl in toolBar.Children)
  54. {
  55. if (ctrl.GetType() == typeof(StackPanel))
  56. {
  57. foreach (Button button in ((Panel)ctrl).Children)
  58. {
  59. check(button);
  60. }
  61. }
  62. else if (ctrl.GetType() == typeof(Button))
  63. {
  64. check((Button)ctrl);
  65. }
  66. }
  67. }
  68.  
  69. /// <summary>
  70. /// converts a string to a DependencyProperty
  71. /// </summary>
  72. /// <param name="propertyString"></param>
  73. /// <returns></returns>
  74. private DependencyProperty getPropertyByString(string propertyString)
  75. {
  76. switch (propertyString)
  77. {
  78. case ("FontStyleProperty"): return FontStyleProperty;
  79. case ("FontWeightProperty"): return FontWeightProperty;
  80. case ("TextDecorations"): return TextBlock.TextDecorationsProperty;
  81. case ("TextAlignment"): return TextBlock.TextAlignmentProperty;
  82. default:
  83. break;
  84. }
  85.  
  86. return null;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement