Ampel.xaml.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using System.Windows.Threading;
  7. using GCHR.Model;
  8. namespace GCHR.View
  9. {
  10. public partial class Ampel
  11. {
  12. private readonly Button[] _buttons;
  13. delegate void DelAsyncWindowAction();
  14. public Ampel()
  15. {
  16. InitializeComponent();
  17. _buttons = new[] { btnStatus, btnGruen, btnGelb, btnRot };
  18. _anzeigeAnStatusAnpassen = delegate()
  19. {
  20. for (var i = 0; i < _buttons.Count(); i++)
  21. {
  22. _buttons[i].Visibility = (i == (int)_status) ? Visibility.Visible : Visibility.Hidden;
  23. }
  24. pbThread.Visibility = (_status == Ampelstatus.Arbeitend) ? Visibility.Visible : Visibility.Hidden;
  25. };
  26. Status = Ampelstatus.Keine;
  27. }
  28. /// <summary>
  29. /// Muss festgelegt werden, dass dynamisch darauf zugegriffen werden kann.
  30. /// </summary>
  31. public static DependencyProperty SourceProperty = DependencyProperty.Register(
  32. "Message", typeof(String), typeof(Ampel));
  33. /// <summary>
  34. /// Gibt an, ob der der Ampel zugehörige Prozess beendet ist.
  35. /// working und rot => false
  36. /// gelb und grün => true
  37. /// </summary>
  38. public bool Done
  39. {
  40. get
  41. {
  42. return (Status == Ampelstatus.Gruen || Status == Ampelstatus.Gelb);
  43. }
  44. }
  45. public Ampelstatus Status
  46. {
  47. get
  48. {
  49. return _status;
  50. }
  51. set
  52. {
  53. _status = value;
  54. Dispatcher.BeginInvoke(DispatcherPriority.Normal, _anzeigeAnStatusAnpassen);
  55. }
  56. }
  57. private Ampelstatus _status;
  58. private readonly DelAsyncWindowAction _anzeigeAnStatusAnpassen;
  59. public int Progress
  60. {
  61. set
  62. {
  63. DelAsyncWindowAction fortschritt = delegate
  64. {
  65. pbThread.Value = value;
  66. pbThread.IsIndeterminate = !(value > 0);
  67. };
  68. pbThread.Dispatcher.BeginInvoke(DispatcherPriority.Normal, fortschritt);
  69. if (Status == Ampelstatus.Keine)
  70. {
  71. Status = Ampelstatus.Arbeitend;
  72. }
  73. if (value == 100 && Status != Ampelstatus.Rot)
  74. {
  75. Status = (Message == "") ? Ampelstatus.Gruen : Ampelstatus.Gelb;
  76. }
  77. }
  78. }
  79. /// <summary>
  80. /// Benutzt <see cref="Ampel.SourceProperty"/>, um den Wert des Statustextes zu schreiben oder zu lesen.
  81. /// </summary>
  82. public string Message
  83. {
  84. get
  85. {
  86. return (String)GetValue(SourceProperty);
  87. }
  88. set
  89. {
  90. SetValue(SourceProperty, value.Trim());
  91. }
  92. }
  93. public void ProgressChanged(int progress, object status)
  94. {
  95. Progress = progress;
  96. if (status is Status)
  97. {
  98. Status = ((Status)status).Ampelstatus;
  99. Message = ((Status)status).Message;
  100. }
  101. else if (status is Ampelstatus)
  102. {
  103. Status = (Ampelstatus)status;
  104. }
  105. else if (status is String && !((String)status).Equals(""))
  106. {
  107. Message += (String)status + Environment.NewLine;
  108. }
  109. }
  110. private void BtnStatusClick(object sender, RoutedEventArgs e)
  111. {
  112. // MessageBox.Show(Message);
  113. }
  114. public void Reset()
  115. {
  116. Status = Ampelstatus.Keine;
  117. Message = "";
  118. }
  119. private void BtnStatusMouseEnter(object sender, MouseEventArgs e)
  120. {
  121. var btn = (Button)sender;
  122. btn.ToolTip = new ToolTip { Content = Message, StaysOpen = true };
  123. }
  124. }
  125. }