StringList.xaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xamarin.Forms;
  7. using Xamarin.Forms.Xaml;
  8. namespace comal.timesheets
  9. {
  10. [XamlCompilation(XamlCompilationOptions.Compile)]
  11. public partial class StringList : ContentView
  12. {
  13. private List<DeleteableEditor> _editors = new List<DeleteableEditor>();
  14. private ImageButton _add;
  15. public StringList()
  16. {
  17. InitializeComponent();
  18. _add = _addbutton;
  19. }
  20. private void AddBtn_Clicked(object sender, EventArgs e)
  21. {
  22. _editors.Add(CreateEditor());
  23. LoadLayout();
  24. }
  25. private void LoadLayout()
  26. {
  27. _grid.Children.Clear();
  28. _grid.RowDefinitions.Clear();
  29. foreach (var editor in _editors)
  30. {
  31. _grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto } );
  32. editor.SetValue(Grid.RowProperty,_grid.RowDefinitions.Count-1);
  33. _grid.Children.Add(editor);
  34. }
  35. if (!_editors.Any())
  36. _grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto } );
  37. _add.SetValue(Grid.RowProperty, _grid.RowDefinitions.Count-1);
  38. _add.SetValue(Grid.ColumnProperty, 1);
  39. _grid.Children.Add(_add);
  40. }
  41. private DeleteableEditor CreateEditor(string text = "")
  42. {
  43. DeleteableEditor editor = new DeleteableEditor(text);
  44. editor.OnEditorDeleted += () =>
  45. {
  46. _editors.Remove(editor);
  47. LoadLayout();
  48. };
  49. return editor;
  50. }
  51. public void LoadList(string[] items)
  52. {
  53. _editors.Clear();
  54. foreach (var item in items)
  55. _editors.Add(CreateEditor(item));
  56. LoadLayout();
  57. }
  58. public string[] SaveItems()
  59. {
  60. List<string> items = new List<string>();
  61. foreach (DeleteableEditor entry in _editors)
  62. {
  63. if (!string.IsNullOrEmpty(entry.Text))
  64. items.Add(entry.Text);
  65. }
  66. return items.ToArray();
  67. }
  68. }
  69. public delegate void EditorDeleted();
  70. public class DeleteableEditor : Grid
  71. {
  72. public event EditorDeleted OnEditorDeleted;
  73. public string Text { get; set; }
  74. public DeleteableEditor(string text = "")
  75. {
  76. Text = text;
  77. Setup();
  78. }
  79. private void Setup()
  80. {
  81. ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
  82. ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  83. ImageButton img = new ImageButton
  84. {
  85. Source = "closee.png",
  86. HeightRequest = 25,
  87. WidthRequest = 25,
  88. };
  89. img.Clicked += (o,e) => OnEditorDeleted?.Invoke();
  90. Grid.SetColumn(img, 0);
  91. var edt = new Entry();
  92. edt.Text = Text;
  93. edt.FontSize = 16;
  94. edt.Keyboard = Keyboard.Plain;
  95. edt.TextChanged += (sender, e) =>
  96. {
  97. Text = edt.Text;
  98. };
  99. Grid.SetColumn(edt, 1);
  100. Children.Add(edt);
  101. Children.Add(img);
  102. }
  103. }
  104. }