ReadonlyGauge.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using Aitex.Core.UI.ControlDataContext;
  15. namespace Aitex.Core.UI.Control
  16. {
  17. public enum ControlTypeEnum
  18. {
  19. MFC,
  20. PC,
  21. PT,
  22. CAL
  23. }
  24. /// <summary>
  25. /// Interaction logic for ReadonlyMFC.xaml
  26. /// </summary>
  27. public partial class ReadonlyGauge : UserControl
  28. {
  29. public ReadonlyGauge()
  30. {
  31. InitializeComponent();
  32. }
  33. /// <summary>
  34. /// define dependency property
  35. /// </summary>
  36. public static readonly DependencyProperty FillBackgroundProperty = DependencyProperty.Register(
  37. "FillBackground", typeof(Brush), typeof(ReadonlyGauge),
  38. new FrameworkPropertyMetadata(Brushes.CadetBlue, FrameworkPropertyMetadataOptions.AffectsRender));
  39. public static readonly DependencyProperty ActualValueProperty = DependencyProperty.Register(
  40. "ActualValue", typeof(ReadonlyGaugeDataItem), typeof(ReadonlyGauge),
  41. new FrameworkPropertyMetadata(default(ReadonlyGaugeDataItem), FrameworkPropertyMetadataOptions.AffectsRender));
  42. public static readonly DependencyProperty ActualAngleProperty = DependencyProperty.Register(
  43. "ActualAngle", typeof(int), typeof(ReadonlyGauge),
  44. new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
  45. public static readonly DependencyProperty ControlTypeProperty = DependencyProperty.Register(
  46. "ControlType", typeof(ControlTypeEnum), typeof(ReadonlyGauge),
  47. new FrameworkPropertyMetadata(ControlTypeEnum.MFC, FrameworkPropertyMetadataOptions.AffectsRender));
  48. public static readonly DependencyProperty NumFontSizeProperty = DependencyProperty.Register(
  49. "NumFontSize", typeof(int), typeof(ReadonlyGauge),
  50. new FrameworkPropertyMetadata(9, FrameworkPropertyMetadataOptions.AffectsRender));
  51. public static readonly DependencyProperty E_notationInvaildProperty = DependencyProperty.Register(
  52. "E_notationInvaild", typeof(bool), typeof(ReadonlyGauge),
  53. new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
  54. public Brush FillBackColor
  55. {
  56. get
  57. {
  58. return (Brush)this.GetValue(FillBackgroundProperty);
  59. }
  60. set
  61. {
  62. this.SetValue(FillBackgroundProperty, value);
  63. }
  64. }
  65. public ReadonlyGaugeDataItem ActualValue
  66. {
  67. get
  68. {
  69. return (ReadonlyGaugeDataItem)this.GetValue(ActualValueProperty);
  70. }
  71. set
  72. {
  73. this.SetValue(ActualValueProperty, value);
  74. }
  75. }
  76. public int ActualAngle
  77. {
  78. get
  79. {
  80. return (int)this.GetValue(ActualAngleProperty);
  81. }
  82. set
  83. {
  84. this.SetValue(ActualAngleProperty, value);
  85. }
  86. }
  87. public ControlTypeEnum ControlType
  88. {
  89. get
  90. {
  91. return (ControlTypeEnum)this.GetValue(ControlTypeProperty);
  92. }
  93. set
  94. {
  95. this.SetValue(ControlTypeProperty, value);
  96. }
  97. }
  98. public int NumFontSize
  99. {
  100. get
  101. {
  102. return (int)this.GetValue(NumFontSizeProperty);
  103. }
  104. set
  105. {
  106. this.SetValue(NumFontSizeProperty, value);
  107. }
  108. }
  109. public bool E_notationInvaild
  110. {
  111. get
  112. {
  113. return (bool)this.GetValue(E_notationInvaildProperty);
  114. }
  115. set
  116. {
  117. this.SetValue(FontSizeProperty, value);
  118. }
  119. }
  120. /// <summary>
  121. /// override rendering behavior
  122. /// </summary>
  123. /// <param name="drawingContext"></param>
  124. protected override void OnRender(DrawingContext drawingContext)
  125. {
  126. base.OnRender(drawingContext);
  127. rectBkground.Fill = FillBackColor;
  128. if (ActualValue != null)
  129. {
  130. this.controlAngle.Angle = ActualAngle;
  131. if (E_notationInvaild)
  132. controlValue.Content = ActualValue.Value.ToString("#.##E+00");
  133. else
  134. controlValue.Content = ActualValue.Value.ToString("0.0");
  135. if (!string.IsNullOrEmpty(ActualValue + ""))
  136. {
  137. this.ToolTip = string.Format("{0}:{1}", ControlType.ToString(), Tag);
  138. }
  139. controlValue.FontSize = NumFontSize;
  140. }
  141. else
  142. {
  143. controlValue.Content = "0.00";
  144. }
  145. }
  146. }
  147. }