DFLabelControl.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. FontWeight = style.IsBold ? FontWeights.Bold : FontWeights.Normal,
  26. FontStyle = style.IsItalic ? FontStyles.Italic : FontStyles.Normal,
  27. VerticalAlignment = VerticalAlignment.Center,
  28. Margin = new Thickness(5)
  29. };
  30. if(style.FontSize > 0)
  31. {
  32. textBlock.FontSize = style.FontSize;
  33. }
  34. if (style.BackgroundColour != System.Drawing.Color.Empty)
  35. {
  36. border.Background = new SolidColorBrush(ConvertColour(style.BackgroundColour));
  37. }
  38. if (style.ForegroundColour != System.Drawing.Color.Empty)
  39. {
  40. textBlock.Foreground = new SolidColorBrush(ConvertColour(style.ForegroundColour));
  41. }
  42. if (style.Underline == UnderlineType.Single)
  43. {
  44. textBlock.TextDecorations.Add(TextDecorations.Underline);
  45. }
  46. else if(style.Underline == UnderlineType.Double)
  47. {
  48. var underline1 = new TextDecoration
  49. {
  50. Pen = new Pen
  51. {
  52. Brush = textBlock.Foreground,
  53. }
  54. };
  55. var underline2 = new TextDecoration
  56. {
  57. Pen = new Pen
  58. {
  59. Brush = textBlock.Foreground,
  60. },
  61. PenOffset = 2
  62. };
  63. textBlock.TextDecorations.Add(underline1);
  64. textBlock.TextDecorations.Add(underline2);
  65. }
  66. border.Child = textBlock;
  67. return border;
  68. }
  69. private static Color ConvertColour(System.Drawing.Color colour)
  70. {
  71. return new Color { R = colour.R, G = colour.G, B = colour.B, A = colour.A };
  72. }
  73. }
  74. }