DFLabelControl.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. namespace InABox.DynamicGrid
  11. {
  12. public class DFLabelControl : DynamicFormControl<DFLayoutLabel>
  13. {
  14. protected override FrameworkElement Create()
  15. {
  16. var border = new Border
  17. {
  18. HorizontalAlignment = HorizontalAlignment.Stretch,
  19. VerticalAlignment = VerticalAlignment.Stretch
  20. };
  21. var style = Control.Style;
  22. var textBlock = new TextBlock
  23. {
  24. Text = Control.Caption,
  25. TextWrapping = TextWrapping.WrapWithOverflow,
  26. FontWeight = style.IsBold ? FontWeights.Bold : FontWeights.Normal,
  27. FontStyle = style.IsItalic ? FontStyles.Italic : FontStyles.Normal,
  28. VerticalAlignment = Control.Style.VerticalTextAlignment switch
  29. {
  30. DFLayoutAlignment.Start => VerticalAlignment.Top,
  31. DFLayoutAlignment.End => VerticalAlignment.Bottom,
  32. DFLayoutAlignment.Stretch => VerticalAlignment.Stretch,
  33. _ => VerticalAlignment.Center
  34. },
  35. HorizontalAlignment = HorizontalAlignment.Stretch,
  36. TextAlignment = Control.Style.HorizontalTextAlignment switch
  37. {
  38. DFLayoutAlignment.Middle => TextAlignment.Center,
  39. DFLayoutAlignment.End => TextAlignment.Right,
  40. DFLayoutAlignment.Stretch => TextAlignment.Justify,
  41. _ => TextAlignment.Left
  42. },
  43. Margin = new Thickness(5)
  44. };
  45. if(style.FontSize > 0)
  46. {
  47. textBlock.FontSize = style.FontSize;
  48. }
  49. if (style.BackgroundColour != System.Drawing.Color.Empty)
  50. {
  51. border.Background = new SolidColorBrush(ConvertColour(style.BackgroundColour));
  52. }
  53. if (style.ForegroundColour != System.Drawing.Color.Empty)
  54. {
  55. textBlock.Foreground = new SolidColorBrush(ConvertColour(style.ForegroundColour));
  56. }
  57. if (style.Underline == UnderlineType.Single)
  58. {
  59. textBlock.TextDecorations.Add(TextDecorations.Underline);
  60. }
  61. else if(style.Underline == UnderlineType.Double)
  62. {
  63. var underline1 = new TextDecoration
  64. {
  65. Pen = new Pen
  66. {
  67. Brush = textBlock.Foreground,
  68. }
  69. };
  70. var underline2 = new TextDecoration
  71. {
  72. Pen = new Pen
  73. {
  74. Brush = textBlock.Foreground,
  75. },
  76. PenOffset = 2
  77. };
  78. textBlock.TextDecorations.Add(underline1);
  79. textBlock.TextDecorations.Add(underline2);
  80. }
  81. border.Child = textBlock;
  82. return border;
  83. }
  84. private static Color ConvertColour(System.Drawing.Color colour)
  85. {
  86. return new Color { R = colour.R, G = colour.G, B = colour.B, A = colour.A };
  87. }
  88. }
  89. }