ExpanderView.xaml.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using Xamarin.CommunityToolkit.UI.Views;
  6. using Xamarin.Forms;
  7. using Xamarin.Forms.Xaml;
  8. using XF.Material.Forms;
  9. namespace PRS.Mobile
  10. {
  11. public delegate void ExpanderViewSelectionChanged(object sender, EventArgs args);
  12. [XamlCompilation(XamlCompilationOptions.Compile)]
  13. public partial class ExpanderView : ContentView
  14. {
  15. private ExpanderViewData _data;
  16. public ExpanderViewData Data
  17. {
  18. get => _data;
  19. set
  20. {
  21. _data = value;
  22. Load();
  23. }
  24. }
  25. public Color SelectedColor { get; set; }
  26. public Color UnselectedColor { get; set; }
  27. public event ExpanderViewSelectionChanged SelectionChanged;
  28. public ExpanderView()
  29. {
  30. InitializeComponent();
  31. SelectedColor = Color.Red;
  32. UnselectedColor = Color.Silver;
  33. }
  34. public void Load()
  35. {
  36. Stack.Children.Clear();
  37. if (Data == null)
  38. return;
  39. foreach (var group in Data.Groups)
  40. {
  41. var expander = new Expander();
  42. expander.HorizontalOptions = LayoutOptions.Start;
  43. expander.Header = CreateHeader(group,(o, e) =>
  44. {
  45. if (o is Frame frame)
  46. {
  47. frame.Margin = new Thickness(0, 0, expander.IsExpanded ? 0 : 20, 0);
  48. //frame.Margin = new Thickness(0, 0, 10, 0);
  49. };
  50. foreach (var exp in Stack.Children.Where(x => x != expander))
  51. exp.IsVisible = expander.IsExpanded;
  52. expander.ForceUpdateSize();
  53. if (!expander.IsExpanded)
  54. Scroll.ScrollToAsync(0, 0, false);
  55. else
  56. Scroll.ScrollToAsync(expander, ScrollToPosition.MakeVisible, false);
  57. });
  58. expander.Content = CreateList(group);
  59. Stack.Children.Add(expander);
  60. }
  61. }
  62. private ContentView CreateHeader(ExpanderViewGroup group, EventHandler ontapped)
  63. {
  64. Frame frame = new Frame();
  65. frame.BackgroundColor = Color.White;
  66. frame.HasShadow = false;
  67. frame.CornerRadius = 5;
  68. frame.BorderColor = Color.Gray;
  69. frame.Padding = new Thickness(2,2,5,2);
  70. frame.HorizontalOptions = LayoutOptions.Fill;
  71. frame.MinimumHeightRequest = 30;
  72. TapGestureRecognizer headerTap = new TapGestureRecognizer() { NumberOfTapsRequired = 1 };
  73. headerTap.Tapped += ontapped;
  74. frame.GestureRecognizers.Add(headerTap);
  75. Grid grid = new Grid();
  76. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
  77. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
  78. if (group.Image != null)
  79. {
  80. Image image = new Image() { Source = group.Image };
  81. image.WidthRequest = 25;
  82. image.HeightRequest = 25;
  83. image.SetValue(Grid.ColumnProperty, 0);
  84. grid.Children.Add(image);
  85. }
  86. Label label = new Label()
  87. {
  88. Text = group.Description,
  89. FontAttributes = FontAttributes.Bold,
  90. FontSize = Device.GetNamedSize(NamedSize.Micro, typeof(Label)),
  91. VerticalTextAlignment = TextAlignment.Center,
  92. HorizontalTextAlignment = TextAlignment.Center
  93. };
  94. label.FontSize = 18;
  95. label.SetValue(Grid.ColumnProperty, 1);
  96. grid.Children.Add(label);
  97. frame.Content = grid;
  98. return frame;
  99. }
  100. private View CreateList(ExpanderViewGroup group)
  101. {
  102. ListView result = new ListView();
  103. result.SelectionMode = ListViewSelectionMode.None;
  104. result.HorizontalOptions = LayoutOptions.Fill;
  105. result.ItemTemplate = new DataTemplate(() =>
  106. {
  107. var frame = new Frame()
  108. {
  109. BackgroundColor = Color.Transparent,
  110. BorderColor = Color.Transparent,
  111. Margin = 0,
  112. Padding = 0,
  113. HasShadow = false
  114. };
  115. var button = new Button
  116. {
  117. MinimumHeightRequest = 30,
  118. TextColor = Color.Black,
  119. FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Button)),
  120. CornerRadius = 5,
  121. BorderColor = UnselectedColor,
  122. BackgroundColor = Material.Color.Surface,
  123. Margin = new Thickness(2, 2, 20, 2),
  124. HorizontalOptions = LayoutOptions.Fill
  125. };
  126. button.SetBinding(Button.TextProperty, "Description");
  127. button.Clicked += ExpanderViewItem_Clicked;
  128. frame.Content = button;
  129. return new ViewCell { View = frame };
  130. });
  131. result.ItemsSource = group.Items;
  132. return result;
  133. }
  134. private void ExpanderViewItem_Clicked(object sender, EventArgs e)
  135. {
  136. var button = (sender as Button);
  137. var item = button.BindingContext as ExpanderViewItem;
  138. item.Selected = !item.Selected;
  139. button.BackgroundColor = item.Selected ? SelectedColor : UnselectedColor;
  140. button.BorderColor = button.BackgroundColor;
  141. SelectionChanged?.Invoke(this,new EventArgs());
  142. }
  143. }
  144. }