JsonEditorControl.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using InABox.Core;
  10. namespace InABox.DynamicGrid;
  11. internal class JsonEditorControl : DynamicEditorControl<string, JsonEditor>
  12. {
  13. static JsonEditorControl()
  14. {
  15. //DynamicEditorControlFactory.Register<JsonEditorControl, JsonEditor>();
  16. }
  17. private Button _button;
  18. private string _value = "";
  19. public JsonEditorControl()
  20. {
  21. Width = 150;
  22. }
  23. public override int DesiredHeight()
  24. {
  25. return 25;
  26. }
  27. public override int DesiredWidth()
  28. {
  29. return 150;
  30. }
  31. public override void SetColor(Color color)
  32. {
  33. _button.Background = new SolidColorBrush(color);
  34. }
  35. public override void SetFocus()
  36. {
  37. // Not Sure what to do here?
  38. }
  39. public override void Configure()
  40. {
  41. }
  42. protected override FrameworkElement CreateEditor()
  43. {
  44. _button = new Button();
  45. _button.Content = "Edit";
  46. _button.Click += _button_Click;
  47. return _button;
  48. }
  49. private void _button_Click(object sender, RoutedEventArgs e)
  50. {
  51. var type = (EditorDefinition as JsonEditor).Type;
  52. var listtype = typeof(List<>).MakeGenericType(type);
  53. var list = Activator.CreateInstance(listtype) as IList;
  54. if (!string.IsNullOrWhiteSpace(_value))
  55. Serialization.DeserializeInto(_value, list);
  56. var array = new object[list.Count];
  57. list.CopyTo(array, 0);
  58. var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicGrid<>), type);
  59. if (grid.EditItems(array))
  60. {
  61. _value = Serialization.Serialize(list, true);
  62. CheckChanged();
  63. }
  64. }
  65. protected override string RetrieveValue()
  66. {
  67. return _value;
  68. }
  69. protected override void UpdateValue(string value)
  70. {
  71. _value = "";
  72. }
  73. }