DFLabelControl.cs 3.3 KB

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