Ampel.xaml.cs 4.1 KB

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