PopupEditorControl.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. namespace InABox.DynamicGrid
  10. {
  11. internal class PopupEditorControl : DynamicEditorControl<Guid>
  12. {
  13. private string _description = "";
  14. private Type _type;
  15. private Guid _value = Guid.Empty;
  16. private TextBox Editor;
  17. //public event DefineFilter OnDefineFilter;
  18. public PopupEditorControl()
  19. {
  20. OtherColumns = new Dictionary<string, string>();
  21. }
  22. public Dictionary<string, string> OtherColumns { get; }
  23. public event OnUpdateOtherEditorHandler OnUpdateOtherEditor;
  24. protected override FrameworkElement CreateEditor()
  25. {
  26. var Grid = new Grid
  27. {
  28. VerticalAlignment = VerticalAlignment.Stretch,
  29. HorizontalAlignment = HorizontalAlignment.Stretch
  30. };
  31. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  32. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(30, GridUnitType.Pixel) });
  33. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50, GridUnitType.Pixel) });
  34. Editor = new TextBox
  35. {
  36. VerticalAlignment = VerticalAlignment.Stretch,
  37. VerticalContentAlignment = VerticalAlignment.Center,
  38. HorizontalAlignment = HorizontalAlignment.Stretch,
  39. IsReadOnly = true
  40. };
  41. Editor.SetValue(Grid.ColumnProperty, 0);
  42. Editor.SetValue(Grid.RowProperty, 0);
  43. Grid.Children.Add(Editor);
  44. var Select = new Button
  45. {
  46. VerticalAlignment = VerticalAlignment.Stretch,
  47. VerticalContentAlignment = VerticalAlignment.Center,
  48. HorizontalAlignment = HorizontalAlignment.Stretch,
  49. Content = "..",
  50. Margin = new Thickness(5, 0, 0, 0),
  51. Focusable = false
  52. };
  53. Select.SetValue(Grid.ColumnProperty, 1);
  54. Select.SetValue(Grid.RowProperty, 0);
  55. Select.Click += Select_Click;
  56. Grid.Children.Add(Select);
  57. var Clear = new Button
  58. {
  59. VerticalAlignment = VerticalAlignment.Stretch,
  60. VerticalContentAlignment = VerticalAlignment.Center,
  61. HorizontalAlignment = HorizontalAlignment.Stretch,
  62. Content = "Clear",
  63. Margin = new Thickness(5, 0, 0, 0),
  64. Focusable = false
  65. };
  66. Clear.SetValue(Grid.ColumnProperty, 2);
  67. Clear.SetValue(Grid.RowProperty, 0);
  68. Clear.Click += Clear_Click;
  69. Grid.Children.Add(Clear);
  70. return Grid;
  71. }
  72. private void Select_Click(object sender, RoutedEventArgs e)
  73. {
  74. if (_type != null)
  75. {
  76. var list = new PopupList(_type, _value, OtherColumns.Keys.ToArray());
  77. //list.OnDefineFilter += (o, t) => { return OnDefineFilter?.Invoke(o, t); };
  78. if (list.ShowDialog() == true)
  79. {
  80. _value = list.ID;
  81. //Dictionary<String, String> fieldmap = new Dictionary<String, String>();
  82. foreach (var key in OtherColumns.Keys)
  83. //String[] comps = othercolumn.Split(new String[] { "->" }, StringSplitOptions.None);
  84. //String field = comps.Last();
  85. //fieldmap[comps.First()] = field;
  86. OtherValues[OtherColumns[key]] = list.OtherValues[key];
  87. //fieldmap[OtherColumns[key]] = list.OtherValues[othercolumn]
  88. CheckChanged();
  89. var display = new List<string>();
  90. //var columns = Entity.DefaultLookupColumns(_type) as IColumns;
  91. var columns = LookupFactory.DefineColumns(_type);
  92. foreach (var column in columns.ColumnNames())
  93. if (list.OtherValues.ContainsKey(column))
  94. display.Add(list.OtherValues[column].ToString());
  95. Editor.Text = string.Join(" / ", display.Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString())));
  96. }
  97. }
  98. }
  99. private void Clear_Click(object sender, RoutedEventArgs e)
  100. {
  101. if (_type != null)
  102. {
  103. _value = Guid.Empty;
  104. foreach (var otherfield in OtherColumns.Keys)
  105. OtherValues[OtherColumns[otherfield]] = null;
  106. CheckChanged();
  107. Editor.Text = "";
  108. }
  109. }
  110. public override int DesiredHeight()
  111. {
  112. return 25;
  113. }
  114. public override int DesiredWidth()
  115. {
  116. return int.MaxValue;
  117. }
  118. protected override Guid RetrieveValue()
  119. {
  120. return _value;
  121. }
  122. protected override void UpdateValue(Guid value)
  123. {
  124. _value = value;
  125. var client = ClientFactory.CreateClient(_type);
  126. var columns = LookupFactory.DefineColumns(_type);
  127. var sort = LookupFactory.DefineSort(_type);
  128. var filter = Activator.CreateInstance(typeof(Filter<>).MakeGenericType(_type));
  129. CoreUtils.SetPropertyValue(filter, "Expression", CoreUtils.GetMemberExpression(_type, "ID"));
  130. CoreUtils.SetPropertyValue(filter, "Operator", Operator.IsEqualTo);
  131. CoreUtils.SetPropertyValue(filter, "Value", value);
  132. var lookup = client.Query(filter, columns, sort);
  133. var display = new List<object>();
  134. if (lookup.Rows.Any())
  135. foreach (var col in lookup.Columns.Where(x => !x.ColumnName.Equals("ID")))
  136. display.Add(lookup.Rows.First()[col.ColumnName]);
  137. Editor.Text = string.Join(" / ", display.Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString())));
  138. }
  139. public override void Configure()
  140. {
  141. base.Configure();
  142. _type = (EditorDefinition as PopupEditor)?.Type;
  143. }
  144. public override void SetFocus()
  145. {
  146. Editor.Focus();
  147. }
  148. public override void SetColor(Color color)
  149. {
  150. Editor.Background = new SolidColorBrush(color);
  151. }
  152. }
  153. }