DynamicGridColumnsEditorControl.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. namespace InABox.DynamicGrid;
  11. public class DynamicGridColumnsEditorControl : DynamicEditorControl<DynamicGridColumns, DynamicGridColumnsEditorAttribute>
  12. {
  13. private TextBox TextBox { get; set; }
  14. private Button Edit { get; set; }
  15. private DynamicGridColumns? _columns;
  16. private DynamicGridColumns? Columns
  17. {
  18. get => _columns;
  19. set
  20. {
  21. _columns = value;
  22. if(value is not null)
  23. {
  24. TextBox.Text = string.Join("; ", value.Select(x => x.ColumnName));
  25. }
  26. else
  27. {
  28. TextBox.Text = "";
  29. }
  30. }
  31. }
  32. private Type? _columnsType { get; set; }
  33. public Type? ColumnsType
  34. {
  35. get => _columnsType;
  36. set
  37. {
  38. if(_columnsType != value)
  39. {
  40. if(value is not null)
  41. {
  42. var cols = DefaultColumns.GetDefaultColumns(value);
  43. Columns = [.. cols.Select(DynamicGridColumn.FromCoreGridColumn)];
  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 schema = new DynamicGridObjectColumnSchema(ColumnsType);
  119. var editor = new DynamicGridColumnsEditor(schema, ColumnsType) { WindowStartupLocation = WindowStartupLocation.CenterScreen };
  120. editor.Columns.AddRange(Columns);
  121. if (editor.ShowDialog().Equals(true))
  122. {
  123. Columns = editor.Columns;
  124. }
  125. }
  126. protected override DynamicGridColumns RetrieveValue()
  127. {
  128. return Columns ?? new();
  129. }
  130. protected override void UpdateValue(DynamicGridColumns value)
  131. {
  132. if (ColumnsType != null)
  133. Columns = value;
  134. else
  135. Columns = null;
  136. }
  137. }