ColumnsEditorControl.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using InABox.Core;
  2. using InABox.Wpf.DynamicGrid.Editors.ColumnsEditor;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Media;
  11. namespace InABox.DynamicGrid;
  12. public class ColumnsEditorControl : DynamicEditorControl<string, ColumnsEditor>
  13. {
  14. private TextBox TextBox { get; set; }
  15. private Button Edit { get; set; }
  16. private IColumns? _columns;
  17. private IColumns? Columns
  18. {
  19. get => _columns;
  20. set
  21. {
  22. _columns = value;
  23. if(value is not null)
  24. {
  25. TextBox.Text = string.Join("; ", value.ColumnNames());
  26. }
  27. else
  28. {
  29. TextBox.Text = "";
  30. }
  31. }
  32. }
  33. private Type? _columnsType { get; set; }
  34. public Type? ColumnsType
  35. {
  36. get => _columnsType;
  37. set
  38. {
  39. if(_columnsType != value)
  40. {
  41. if(value is not null)
  42. {
  43. Columns = Core.Columns.Create(value, ColumnTypeFlags.IncludeVisible);
  44. }
  45. else
  46. {
  47. Columns = null;
  48. }
  49. }
  50. _columnsType = value;
  51. if(Edit != null)
  52. {
  53. Edit.IsEnabled = value != null;
  54. }
  55. CheckChanged();
  56. }
  57. }
  58. public override int DesiredHeight()
  59. {
  60. return 25;
  61. }
  62. public override int DesiredWidth()
  63. {
  64. return int.MaxValue;
  65. }
  66. public override void SetColor(Color color)
  67. {
  68. TextBox.Background = new SolidColorBrush(color);
  69. }
  70. public override void SetFocus()
  71. {
  72. TextBox.Focus();
  73. }
  74. public override void Configure()
  75. {
  76. }
  77. protected override FrameworkElement CreateEditor()
  78. {
  79. var grid = new Grid
  80. {
  81. VerticalAlignment = VerticalAlignment.Stretch,
  82. HorizontalAlignment = HorizontalAlignment.Stretch
  83. };
  84. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  85. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
  86. TextBox = new TextBox
  87. {
  88. VerticalAlignment = VerticalAlignment.Stretch,
  89. VerticalContentAlignment = VerticalAlignment.Center,
  90. HorizontalAlignment = HorizontalAlignment.Stretch,
  91. Margin = new Thickness(0, 0, 0, 0),
  92. IsEnabled = false
  93. };
  94. TextBox.SetValue(Grid.ColumnProperty, 0);
  95. TextBox.SetValue(Grid.RowProperty, 0);
  96. Edit = new Button
  97. {
  98. VerticalAlignment = VerticalAlignment.Stretch,
  99. VerticalContentAlignment = VerticalAlignment.Center,
  100. HorizontalAlignment = HorizontalAlignment.Stretch,
  101. Margin = new Thickness(5, 1, 0, 1),
  102. Width = 45,
  103. Content = "Edit",
  104. Focusable = false,
  105. IsEnabled = ColumnsType != null
  106. };
  107. Edit.SetValue(Grid.ColumnProperty, 1);
  108. Edit.SetValue(Grid.RowProperty, 0);
  109. Edit.Click += EditButton_Click;
  110. grid.Children.Add(TextBox);
  111. grid.Children.Add(Edit);
  112. ColumnsType = EditorDefinition.Type;
  113. return grid;
  114. }
  115. private void EditButton_Click(object sender, RoutedEventArgs e)
  116. {
  117. if (ColumnsType is null || Columns is null) return;
  118. var grid = new ColumnsEditorGrid(ColumnsType, Columns);
  119. grid.Refresh(true, true);
  120. var dlg = new DynamicContentDialog(grid)
  121. {
  122. Title = "Edit Columns",
  123. Width = 400,
  124. Height = 600,
  125. WindowStartupLocation = WindowStartupLocation.CenterScreen
  126. };
  127. grid.OnChanged += (o, e) =>
  128. {
  129. dlg.CanSave = true;
  130. };
  131. if(dlg.ShowDialog() == true)
  132. {
  133. Columns = grid.GetColumns();
  134. CheckChanged();
  135. }
  136. }
  137. protected override string RetrieveValue()
  138. {
  139. return Columns != null ? Serialization.Serialize(Columns) : "";
  140. }
  141. protected override void UpdateValue(string value)
  142. {
  143. if (ColumnsType != null)
  144. Columns = Serialization.Deserialize(typeof(Columns<>).MakeGenericType(ColumnsType), value) as IColumns
  145. ?? Core.Columns.Create(ColumnsType, ColumnTypeFlags.IncludeVisible);
  146. else
  147. Columns = null;
  148. }
  149. }