|
| 1 | +using System; |
| 2 | +using Microsoft.Maui.Controls; |
| 3 | +using System.Collections.ObjectModel; |
| 4 | +using Maui.Controls.Sample.CollectionViewGalleries; |
| 5 | + |
| 6 | +namespace Maui.Controls.Sample |
| 7 | +{ |
| 8 | + public partial class CollectionViewOptionsPage : ContentPage |
| 9 | + { |
| 10 | + private CollectionViewViewModel _viewModel; |
| 11 | + |
| 12 | + public CollectionViewOptionsPage(CollectionViewViewModel viewModel) |
| 13 | + { |
| 14 | + InitializeComponent(); |
| 15 | + _viewModel = viewModel; |
| 16 | + BindingContext = _viewModel; |
| 17 | + } |
| 18 | + |
| 19 | + private void ApplyButton_Clicked(object sender, EventArgs e) |
| 20 | + { |
| 21 | + Navigation.PopAsync(); |
| 22 | + } |
| 23 | + |
| 24 | + private void OnEmptyViewChanged(object sender, CheckedChangedEventArgs e) |
| 25 | + { |
| 26 | + if (EmptyViewNone.IsChecked) |
| 27 | + { |
| 28 | + _viewModel.EmptyView = null; |
| 29 | + } |
| 30 | + else if (EmptyViewString.IsChecked) |
| 31 | + { |
| 32 | + _viewModel.EmptyView = "No Items Available(String)"; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private void OnHeaderChanged(object sender, CheckedChangedEventArgs e) |
| 37 | + { |
| 38 | + if (HeaderNone.IsChecked) |
| 39 | + { |
| 40 | + _viewModel.Header = null; |
| 41 | + } |
| 42 | + else if (HeaderString.IsChecked) |
| 43 | + { |
| 44 | + _viewModel.Header = "CollectionView Header(String)"; |
| 45 | + } |
| 46 | + else if (HeaderGrid.IsChecked) |
| 47 | + { |
| 48 | + Grid grid = new Grid |
| 49 | + { |
| 50 | + BackgroundColor = Colors.LightGray, |
| 51 | + Padding = new Thickness(10) |
| 52 | + }; |
| 53 | + grid.Children.Add(new Label |
| 54 | + { |
| 55 | + Text = "CollectionView Header(Grid View)", |
| 56 | + FontSize = 18, |
| 57 | + HorizontalOptions = LayoutOptions.Center, |
| 58 | + VerticalOptions = LayoutOptions.Center, |
| 59 | + TextColor = Colors.Blue, |
| 60 | + AutomationId = "HeaderViewLabel" |
| 61 | + }); |
| 62 | + _viewModel.Header = grid; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private void OnFooterChanged(object sender, CheckedChangedEventArgs e) |
| 67 | + { |
| 68 | + if (FooterNone.IsChecked) |
| 69 | + { |
| 70 | + _viewModel.Footer = null; |
| 71 | + } |
| 72 | + else if (FooterString.IsChecked) |
| 73 | + { |
| 74 | + _viewModel.Footer = "CollectionView Footer(String)"; |
| 75 | + } |
| 76 | + else if (FooterGrid.IsChecked) |
| 77 | + { |
| 78 | + Grid grid = new Grid |
| 79 | + { |
| 80 | + BackgroundColor = Colors.LightGray, |
| 81 | + Padding = new Thickness(10) |
| 82 | + }; |
| 83 | + grid.Children.Add(new Label |
| 84 | + { |
| 85 | + Text = "CollectionView Footer(Grid View)", |
| 86 | + FontSize = 18, |
| 87 | + HorizontalOptions = LayoutOptions.Center, |
| 88 | + VerticalOptions = LayoutOptions.Center, |
| 89 | + TextColor = Colors.Red, |
| 90 | + AutomationId = "FooterViewLabel" |
| 91 | + }); |
| 92 | + _viewModel.Footer = grid; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private void OnHeaderTemplateChanged(object sender, CheckedChangedEventArgs e) |
| 97 | + { |
| 98 | + if (HeaderTemplateNone.IsChecked) |
| 99 | + { |
| 100 | + _viewModel.HeaderTemplate = null; |
| 101 | + } |
| 102 | + else if (HeaderTemplateGrid.IsChecked) |
| 103 | + { |
| 104 | + _viewModel.HeaderTemplate = new DataTemplate(() => |
| 105 | + { |
| 106 | + Grid grid = new Grid |
| 107 | + { |
| 108 | + BackgroundColor = Colors.LightGray, |
| 109 | + Padding = new Thickness(10) |
| 110 | + }; |
| 111 | + grid.Children.Add(new Label |
| 112 | + { |
| 113 | + Text = "Header Template(Grid View)", |
| 114 | + FontSize = 18, |
| 115 | + FontAttributes = FontAttributes.Bold, |
| 116 | + HorizontalOptions = LayoutOptions.Center, |
| 117 | + VerticalOptions = LayoutOptions.Center, |
| 118 | + TextColor = Colors.Blue, |
| 119 | + AutomationId = "HeaderTemplateLabel" |
| 120 | + }); |
| 121 | + return grid; |
| 122 | + }); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private void OnFooterTemplateChanged(object sender, CheckedChangedEventArgs e) |
| 127 | + { |
| 128 | + if (FooterTemplateNone.IsChecked) |
| 129 | + { |
| 130 | + _viewModel.FooterTemplate = null; |
| 131 | + } |
| 132 | + else if (FooterTemplateGrid.IsChecked) |
| 133 | + { |
| 134 | + _viewModel.FooterTemplate = new DataTemplate(() => |
| 135 | + { |
| 136 | + Grid grid = new Grid |
| 137 | + { |
| 138 | + BackgroundColor = Colors.LightGray, |
| 139 | + Padding = new Thickness(10) |
| 140 | + }; |
| 141 | + grid.Children.Add(new Label |
| 142 | + { |
| 143 | + Text = "Footer Template(Grid View)", |
| 144 | + FontSize = 18, |
| 145 | + FontAttributes = FontAttributes.Bold, |
| 146 | + HorizontalOptions = LayoutOptions.Center, |
| 147 | + VerticalOptions = LayoutOptions.Center, |
| 148 | + TextColor = Colors.Green, |
| 149 | + AutomationId = "FooterTemplateLabel" |
| 150 | + }); |
| 151 | + return grid; |
| 152 | + }); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private void OnGroupHeaderTemplateChanged(object sender, CheckedChangedEventArgs e) |
| 157 | + { |
| 158 | + if (GroupHeaderTemplateNone.IsChecked) |
| 159 | + { |
| 160 | + _viewModel.GroupHeaderTemplate = null; |
| 161 | + } |
| 162 | + else if (GroupHeaderTemplateGrid.IsChecked) |
| 163 | + { |
| 164 | + _viewModel.GroupHeaderTemplate = new DataTemplate(() => |
| 165 | + { |
| 166 | + Grid grid = new Grid |
| 167 | + { |
| 168 | + BackgroundColor = Colors.LightGray, |
| 169 | + Padding = new Thickness(10) |
| 170 | + }; |
| 171 | + grid.Children.Add(new Label |
| 172 | + { |
| 173 | + Text = "Group Header Template(Grid View)", |
| 174 | + FontSize = 18, |
| 175 | + FontAttributes = FontAttributes.Bold, |
| 176 | + HorizontalOptions = LayoutOptions.Center, |
| 177 | + VerticalOptions = LayoutOptions.Center, |
| 178 | + TextColor = Colors.Green, |
| 179 | + AutomationId = "GroupHeaderTemplateLabel" |
| 180 | + }); |
| 181 | + return grid; |
| 182 | + }); |
| 183 | + } |
| 184 | + } |
| 185 | + private void OnGroupFooterTemplateChanged(object sender, CheckedChangedEventArgs e) |
| 186 | + { |
| 187 | + if (GroupFooterTemplateNone.IsChecked) |
| 188 | + { |
| 189 | + _viewModel.GroupFooterTemplate = null; |
| 190 | + } |
| 191 | + else if (GroupFooterTemplateGrid.IsChecked) |
| 192 | + { |
| 193 | + _viewModel.GroupFooterTemplate = new DataTemplate(() => |
| 194 | + { |
| 195 | + Grid grid = new Grid |
| 196 | + { |
| 197 | + BackgroundColor = Colors.LightGray, |
| 198 | + Padding = new Thickness(10) |
| 199 | + }; |
| 200 | + grid.Children.Add(new Label |
| 201 | + { |
| 202 | + Text = "Group Footer Template(Grid View)", |
| 203 | + FontSize = 18, |
| 204 | + FontAttributes = FontAttributes.Bold, |
| 205 | + HorizontalOptions = LayoutOptions.Center, |
| 206 | + VerticalOptions = LayoutOptions.Center, |
| 207 | + TextColor = Colors.Red, |
| 208 | + AutomationId = "GroupFooterTemplateLabel" |
| 209 | + }); |
| 210 | + return grid; |
| 211 | + }); |
| 212 | + } |
| 213 | + } |
| 214 | + private void OnIsGroupedChanged(object sender, CheckedChangedEventArgs e) |
| 215 | + { |
| 216 | + if (IsGroupedFalse.IsChecked) |
| 217 | + { |
| 218 | + _viewModel.IsGrouped = false; |
| 219 | + } |
| 220 | + else if (IsGroupedTrue.IsChecked) |
| 221 | + { |
| 222 | + _viewModel.IsGrouped = true; |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + private void OnItemTemplateChanged(object sender, CheckedChangedEventArgs e) |
| 227 | + { |
| 228 | + if (ItemTemplateNone.IsChecked) |
| 229 | + { |
| 230 | + _viewModel.ItemTemplate = null; |
| 231 | + } |
| 232 | + else if (ItemTemplateBasic.IsChecked) |
| 233 | + { |
| 234 | + _viewModel.ItemTemplate = new DataTemplate(() => |
| 235 | + { |
| 236 | + var label = new Label(); |
| 237 | + label.SetBinding(Label.TextProperty, new Binding("Caption")); |
| 238 | + |
| 239 | + return label; |
| 240 | + }); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + private void OnItemsLayoutChanged(object sender, CheckedChangedEventArgs e) |
| 245 | + { |
| 246 | + if (ItemsLayoutVerticalList.IsChecked) |
| 247 | + { |
| 248 | + _viewModel.ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Vertical); |
| 249 | + } |
| 250 | + else if (ItemsLayoutHorizontalList.IsChecked) |
| 251 | + { |
| 252 | + _viewModel.ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal); |
| 253 | + } |
| 254 | + else if (ItemsLayoutVerticalGrid.IsChecked) |
| 255 | + { |
| 256 | + _viewModel.ItemsLayout = new GridItemsLayout(2, ItemsLayoutOrientation.Vertical); // 2 columns |
| 257 | + } |
| 258 | + else if (ItemsLayoutHorizontalGrid.IsChecked) |
| 259 | + { |
| 260 | + _viewModel.ItemsLayout = new GridItemsLayout(2, ItemsLayoutOrientation.Horizontal); // 2 rows |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + private void OnItemsSourceChanged(object sender, CheckedChangedEventArgs e) |
| 265 | + { |
| 266 | + if (!(sender is RadioButton radioButton) || !e.Value) |
| 267 | + return; |
| 268 | + if (radioButton == ItemsSourceObservableCollection25) |
| 269 | + _viewModel.ItemsSourceType = ItemsSourceType.ObservableCollection25T; |
| 270 | + else if (radioButton == ItemsSourceObservableCollection5) |
| 271 | + _viewModel.ItemsSourceType = ItemsSourceType.ObservableCollection5T; |
| 272 | + else if (radioButton == ItemsSourceGroupedList) |
| 273 | + _viewModel.ItemsSourceType = ItemsSourceType.GroupedListT; |
| 274 | + else if (radioButton == ItemsSourceNone) |
| 275 | + _viewModel.ItemsSourceType = ItemsSourceType.None; |
| 276 | + } |
| 277 | + } |
| 278 | +} |
0 commit comments