DFLabelControl.cs 3.0 KB

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