NotesEditorControl.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Data;
  6. using System.Windows.Media;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. namespace InABox.DynamicGrid
  10. {
  11. public class TextBoxDecorator : Decorator
  12. {
  13. // properties
  14. public override UIElement Child
  15. {
  16. get => base.Child;
  17. set
  18. {
  19. var oldValue = base.Child;
  20. if (oldValue != null)
  21. {
  22. //var wbind = BindingOperations.GetBinding(oldValue, FrameworkElement.WidthProperty);
  23. //if ((wbind != null) && (wbind.Source == this))
  24. // BindingOperations.ClearBinding(oldValue, FrameworkElement.WidthProperty);
  25. var hbind = BindingOperations.GetBinding(oldValue, HeightProperty);
  26. if (hbind != null && hbind.Source == this)
  27. BindingOperations.ClearBinding(oldValue, HeightProperty);
  28. }
  29. base.Child = value;
  30. if (value != null &&
  31. BindingOperations.GetBinding(value, HeightProperty) == null)
  32. BindingOperations.SetBinding(
  33. value,
  34. HeightProperty,
  35. new Binding
  36. {
  37. Source = this,
  38. Path = new PropertyPath(ActualHeightProperty),
  39. Mode = BindingMode.OneWay
  40. });
  41. }
  42. }
  43. // methods
  44. protected override Size MeasureOverride(Size constraint)
  45. {
  46. var result = base.MeasureOverride(constraint);
  47. if (double.IsInfinity(constraint.Height))
  48. result.Height = (Child as FrameworkElement)?.MinHeight ?? 0.0;
  49. return result;
  50. }
  51. }
  52. public class NotesEditorControl : DynamicEditorControl<string[]>
  53. {
  54. private Color BGColor = Colors.Gainsboro;
  55. //private TextBox Editor = null;
  56. private Button Button;
  57. private Grid Grid;
  58. private TextBox History;
  59. private TextBoxDecorator Scroll;
  60. public override int DesiredHeight()
  61. {
  62. return int.MaxValue;
  63. }
  64. public override int DesiredWidth()
  65. {
  66. return int.MaxValue;
  67. }
  68. public override void SetColor(Color color)
  69. {
  70. BGColor = color;
  71. if (!History.IsReadOnly)
  72. History.Background = new SolidColorBrush(color);
  73. }
  74. public override void SetFocus()
  75. {
  76. Button.Focus();
  77. }
  78. protected override FrameworkElement CreateEditor()
  79. {
  80. VerticalAlignment = VerticalAlignment.Stretch;
  81. MinHeight = 250;
  82. Grid = new Grid();
  83. Grid.VerticalAlignment = VerticalAlignment.Stretch;
  84. Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  85. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  86. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
  87. Scroll = new TextBoxDecorator
  88. {
  89. VerticalAlignment = VerticalAlignment.Stretch,
  90. HorizontalAlignment = HorizontalAlignment.Stretch
  91. //HorizontalContentAlignment = HorizontalAlignment.Stretch,
  92. //VerticalScrollBarVisibility = ScrollBarVisibility.Visible,
  93. //HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled,
  94. //Background = new SolidColorBrush(Colors.Yellow)
  95. };
  96. Scroll.SetValue(Grid.RowProperty, 0);
  97. Scroll.SetValue(Grid.ColumnProperty, 0);
  98. Grid.Children.Add(Scroll);
  99. History = new TextBox
  100. {
  101. VerticalAlignment = VerticalAlignment.Top,
  102. VerticalContentAlignment = VerticalAlignment.Stretch,
  103. HorizontalAlignment = HorizontalAlignment.Stretch,
  104. TextWrapping = TextWrapping.Wrap,
  105. AcceptsReturn = true,
  106. IsReadOnly = true,
  107. VerticalScrollBarVisibility = ScrollBarVisibility.Auto
  108. };
  109. History.IsVisibleChanged += (o, e) =>
  110. {
  111. History.Focus();
  112. History.CaretIndex = History.Text.Length;
  113. History.ScrollToEnd();
  114. };
  115. History.TextChanged += (o, e) =>
  116. {
  117. if (History.IsReadOnly)
  118. {
  119. History.Focus();
  120. History.CaretIndex = History.Text.Length;
  121. History.ScrollToEnd();
  122. }
  123. else
  124. {
  125. CheckChanged();
  126. }
  127. };
  128. Scroll.Child = History;
  129. Button = new Button
  130. {
  131. VerticalAlignment = VerticalAlignment.Stretch,
  132. VerticalContentAlignment = VerticalAlignment.Center,
  133. HorizontalAlignment = HorizontalAlignment.Stretch,
  134. HorizontalContentAlignment = HorizontalAlignment.Center,
  135. Content = "Add",
  136. Width = 50
  137. };
  138. Button.SetValue(Grid.RowProperty, 0);
  139. Button.SetValue(Grid.ColumnProperty, 1);
  140. Button.Click += Button_Click;
  141. Button.Margin = new Thickness(5, 0, 0, 0);
  142. Grid.Children.Add(Button);
  143. return Grid;
  144. }
  145. private void Button_Click(object sender, RoutedEventArgs e)
  146. {
  147. var popup = new NotePopup { Caption = "Add Note", Text = "" };
  148. if (popup.ShowDialog() == true)
  149. {
  150. if (string.IsNullOrEmpty(History.Text))
  151. {
  152. History.AppendText(popup.Text);
  153. }
  154. else
  155. {
  156. var note = string.Format("{0:yyyy-MM-dd HH:mm:ss} {1}: {2}", DateTime.Now, ClientFactory.UserID, popup.Text);
  157. History.AppendText("\n===================================\n" + note);
  158. }
  159. History.ScrollToEnd();
  160. CheckChanged();
  161. }
  162. }
  163. protected override string[] RetrieveValue()
  164. {
  165. var results = new List<string>();
  166. if (!string.IsNullOrWhiteSpace(History.Text))
  167. results.AddRange(History.Text.Split('\n'));
  168. return results.ToArray();
  169. }
  170. protected override void UpdateValue(string[] value)
  171. {
  172. if (value != null)
  173. History.Text = string.Join("\n", value);
  174. else
  175. History.Text = "";
  176. var AlwaysEnabled = EditorDefinition is NotesEditor && ((NotesEditor)EditorDefinition).AlwaysEnabled;
  177. History.IsReadOnly = !AlwaysEnabled && !string.IsNullOrWhiteSpace(History.Text);
  178. History.Background = History.IsReadOnly ? new SolidColorBrush(Colors.Gainsboro) : new SolidColorBrush(BGColor);
  179. Button.Visibility = History.IsReadOnly ? Visibility.Visible : Visibility.Collapsed;
  180. History.Focus();
  181. History.CaretIndex = History.Text.Length;
  182. History.ScrollToEnd();
  183. }
  184. }
  185. }