forked from dotnet/maui
/
ListView.cs
768 lines (629 loc) · 29.5 KB
/
ListView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#nullable disable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Xaml.Diagnostics;
using Microsoft.Maui.Devices;
using Microsoft.Maui.Graphics;
namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="Type[@FullName='Microsoft.Maui.Controls.ListView']/Docs/*" />
public class ListView : ItemsView<Cell>, IListViewController, IElementConfiguration<ListView>, IVisualTreeElement
{
// The ListViewRenderer has some odd behavior with LogicalChildren
// https://github.com/xamarin/Xamarin.Forms/pull/12057
// Ideally we'd fix the ListViewRenderer so we don't have this separation
readonly List<Element> _visualChildren = new List<Element>();
IReadOnlyList<IVisualTreeElement> IVisualTreeElement.GetVisualChildren() => _visualChildren;
/// <summary>Bindable property for <see cref="IsPullToRefreshEnabled"/>.</summary>
public static readonly BindableProperty IsPullToRefreshEnabledProperty = BindableProperty.Create("IsPullToRefreshEnabled", typeof(bool), typeof(ListView), false);
/// <summary>Bindable property for <see cref="IsRefreshing"/>.</summary>
public static readonly BindableProperty IsRefreshingProperty = BindableProperty.Create("IsRefreshing", typeof(bool), typeof(ListView), false, BindingMode.TwoWay);
/// <summary>Bindable property for <see cref="RefreshCommand"/>.</summary>
public static readonly BindableProperty RefreshCommandProperty = BindableProperty.Create("RefreshCommand", typeof(ICommand), typeof(ListView), null, propertyChanged: OnRefreshCommandChanged);
/// <summary>Bindable property for <see cref="Header"/>.</summary>
public static readonly BindableProperty HeaderProperty = BindableProperty.Create("Header", typeof(object), typeof(ListView), null, propertyChanged: OnHeaderChanged);
/// <summary>Bindable property for <see cref="HeaderTemplate"/>.</summary>
public static readonly BindableProperty HeaderTemplateProperty = BindableProperty.Create("HeaderTemplate", typeof(DataTemplate), typeof(ListView), null, propertyChanged: OnHeaderTemplateChanged,
validateValue: ValidateHeaderFooterTemplate);
/// <summary>Bindable property for <see cref="Footer"/>.</summary>
public static readonly BindableProperty FooterProperty = BindableProperty.Create("Footer", typeof(object), typeof(ListView), null, propertyChanged: OnFooterChanged);
/// <summary>Bindable property for <see cref="FooterTemplate"/>.</summary>
public static readonly BindableProperty FooterTemplateProperty = BindableProperty.Create("FooterTemplate", typeof(DataTemplate), typeof(ListView), null, propertyChanged: OnFooterTemplateChanged,
validateValue: ValidateHeaderFooterTemplate);
/// <summary>Bindable property for <see cref="SelectedItem"/>.</summary>
public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create("SelectedItem", typeof(object), typeof(ListView), null, BindingMode.OneWayToSource,
propertyChanged: OnSelectedItemChanged);
/// <summary>Bindable property for <see cref="SelectionMode"/>.</summary>
public static readonly BindableProperty SelectionModeProperty = BindableProperty.Create(nameof(SelectionMode), typeof(ListViewSelectionMode), typeof(ListView), ListViewSelectionMode.Single);
/// <summary>Bindable property for <see cref="HasUnevenRows"/>.</summary>
public static readonly BindableProperty HasUnevenRowsProperty = BindableProperty.Create("HasUnevenRows", typeof(bool), typeof(ListView), false);
/// <summary>Bindable property for <see cref="RowHeight"/>.</summary>
public static readonly BindableProperty RowHeightProperty = BindableProperty.Create("RowHeight", typeof(int), typeof(ListView), -1);
/// <summary>Bindable property for <see cref="GroupHeaderTemplate"/>.</summary>
public static readonly BindableProperty GroupHeaderTemplateProperty = BindableProperty.Create("GroupHeaderTemplate", typeof(DataTemplate), typeof(ListView), null,
propertyChanged: OnGroupHeaderTemplateChanged);
/// <summary>Bindable property for <see cref="IsGroupingEnabled"/>.</summary>
public static readonly BindableProperty IsGroupingEnabledProperty = BindableProperty.Create("IsGroupingEnabled", typeof(bool), typeof(ListView), false);
/// <summary>Bindable property for <see cref="SeparatorVisibility"/>.</summary>
public static readonly BindableProperty SeparatorVisibilityProperty = BindableProperty.Create("SeparatorVisibility", typeof(SeparatorVisibility), typeof(ListView), SeparatorVisibility.Default);
/// <summary>Bindable property for <see cref="SeparatorColor"/>.</summary>
public static readonly BindableProperty SeparatorColorProperty = BindableProperty.Create("SeparatorColor", typeof(Color), typeof(ListView), null);
/// <summary>Bindable property for <see cref="RefreshControlColor"/>.</summary>
public static readonly BindableProperty RefreshControlColorProperty = BindableProperty.Create(nameof(RefreshControlColor), typeof(Color), typeof(ListView), null);
/// <summary>Bindable property for <see cref="HorizontalScrollBarVisibility"/>.</summary>
public static readonly BindableProperty HorizontalScrollBarVisibilityProperty = BindableProperty.Create(nameof(HorizontalScrollBarVisibility), typeof(ScrollBarVisibility), typeof(ListView), ScrollBarVisibility.Default);
/// <summary>Bindable property for <see cref="VerticalScrollBarVisibility"/>.</summary>
public static readonly BindableProperty VerticalScrollBarVisibilityProperty = BindableProperty.Create(nameof(VerticalScrollBarVisibility), typeof(ScrollBarVisibility), typeof(ListView), ScrollBarVisibility.Default);
static readonly ToStringValueConverter _toStringValueConverter = new ToStringValueConverter();
readonly Lazy<PlatformConfigurationRegistry<ListView>> _platformConfigurationRegistry;
BindingBase _groupDisplayBinding;
BindingBase _groupShortNameBinding;
Element _headerElement;
Element _footerElement;
ScrollToRequestedEventArgs _pendingScroll;
int _previousGroupSelected = -1;
int _previousRowSelected = -1;
/// <summary>
/// Controls whether anything happens in BeginRefresh(), is set based on RefreshCommand.CanExecute
/// </summary>
bool _refreshAllowed = true;
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='.ctor'][1]/Docs/*" />
public ListView()
{
#pragma warning disable CS0618 // Type or member is obsolete
VerticalOptions = HorizontalOptions = LayoutOptions.FillAndExpand;
#pragma warning restore CS0618 // Type or member is obsolete
TemplatedItems.IsGroupingEnabledProperty = IsGroupingEnabledProperty;
TemplatedItems.GroupHeaderTemplateProperty = GroupHeaderTemplateProperty;
_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<ListView>>(() => new PlatformConfigurationRegistry<ListView>(this));
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='.ctor']/Docs/*" />
public ListView([Parameter("CachingStrategy")] ListViewCachingStrategy cachingStrategy) : this()
{
// Unknown => UnitTest "platform"
if (DeviceInfo.Platform == DevicePlatform.Unknown ||
DeviceInfo.Platform == DevicePlatform.Android ||
DeviceInfo.Platform == DevicePlatform.iOS ||
DeviceInfo.Platform == DevicePlatform.macOS)
CachingStrategy = cachingStrategy;
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='Footer']/Docs/*" />
public object Footer
{
get { return GetValue(FooterProperty); }
set { SetValue(FooterProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='FooterTemplate']/Docs/*" />
public DataTemplate FooterTemplate
{
get { return (DataTemplate)GetValue(FooterTemplateProperty); }
set { SetValue(FooterTemplateProperty, value); }
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
object bc = BindingContext;
if (Header is Element header)
SetChildInheritedBindingContext(header, bc);
if (Footer is Element footer)
SetChildInheritedBindingContext(footer, bc);
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='GroupDisplayBinding']/Docs/*" />
public BindingBase GroupDisplayBinding
{
get { return _groupDisplayBinding; }
set
{
if (_groupDisplayBinding == value)
return;
OnPropertyChanging();
BindingBase oldValue = value;
_groupDisplayBinding = value;
OnGroupDisplayBindingChanged(this, oldValue, _groupDisplayBinding);
TemplatedItems.GroupDisplayBinding = value;
OnPropertyChanged();
}
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='GroupHeaderTemplate']/Docs/*" />
public DataTemplate GroupHeaderTemplate
{
get { return (DataTemplate)GetValue(GroupHeaderTemplateProperty); }
set { SetValue(GroupHeaderTemplateProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='GroupShortNameBinding']/Docs/*" />
public BindingBase GroupShortNameBinding
{
get { return _groupShortNameBinding; }
set
{
if (_groupShortNameBinding == value)
return;
OnPropertyChanging();
_groupShortNameBinding = value;
TemplatedItems.GroupShortNameBinding = value;
OnPropertyChanged();
}
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='HasUnevenRows']/Docs/*" />
public bool HasUnevenRows
{
get { return (bool)GetValue(HasUnevenRowsProperty); }
set { SetValue(HasUnevenRowsProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='Header']/Docs/*" />
public object Header
{
get { return GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='HeaderTemplate']/Docs/*" />
public DataTemplate HeaderTemplate
{
get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
set { SetValue(HeaderTemplateProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='IsGroupingEnabled']/Docs/*" />
public bool IsGroupingEnabled
{
get { return (bool)GetValue(IsGroupingEnabledProperty); }
set { SetValue(IsGroupingEnabledProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='IsPullToRefreshEnabled']/Docs/*" />
public bool IsPullToRefreshEnabled
{
get { return (bool)GetValue(IsPullToRefreshEnabledProperty); }
set { SetValue(IsPullToRefreshEnabledProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='IsRefreshing']/Docs/*" />
public bool IsRefreshing
{
get { return (bool)GetValue(IsRefreshingProperty); }
set { SetValue(IsRefreshingProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='RefreshCommand']/Docs/*" />
public ICommand RefreshCommand
{
get { return (ICommand)GetValue(RefreshCommandProperty); }
set { SetValue(RefreshCommandProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='RowHeight']/Docs/*" />
public int RowHeight
{
get { return (int)GetValue(RowHeightProperty); }
set { SetValue(RowHeightProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='SelectedItem']/Docs/*" />
public object SelectedItem
{
get { return GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='SelectionMode']/Docs/*" />
public ListViewSelectionMode SelectionMode
{
get { return (ListViewSelectionMode)GetValue(SelectionModeProperty); }
set { SetValue(SelectionModeProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='SeparatorColor']/Docs/*" />
public Color SeparatorColor
{
get { return (Color)GetValue(SeparatorColorProperty); }
set { SetValue(SeparatorColorProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='RefreshControlColor']/Docs/*" />
public Color RefreshControlColor
{
get { return (Color)GetValue(RefreshControlColorProperty); }
set { SetValue(RefreshControlColorProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='SeparatorVisibility']/Docs/*" />
public SeparatorVisibility SeparatorVisibility
{
get { return (SeparatorVisibility)GetValue(SeparatorVisibilityProperty); }
set { SetValue(SeparatorVisibilityProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='HorizontalScrollBarVisibility']/Docs/*" />
public ScrollBarVisibility HorizontalScrollBarVisibility
{
get { return (ScrollBarVisibility)GetValue(HorizontalScrollBarVisibilityProperty); }
set { SetValue(HorizontalScrollBarVisibilityProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='VerticalScrollBarVisibility']/Docs/*" />
public ScrollBarVisibility VerticalScrollBarVisibility
{
get { return (ScrollBarVisibility)GetValue(VerticalScrollBarVisibilityProperty); }
set { SetValue(VerticalScrollBarVisibilityProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='CachingStrategy']/Docs/*" />
public ListViewCachingStrategy CachingStrategy { get; private set; }
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='RefreshAllowed']/Docs/*" />
[EditorBrowsable(EditorBrowsableState.Never)]
public bool RefreshAllowed
{
set
{
if (_refreshAllowed == value)
return;
_refreshAllowed = value;
OnPropertyChanged();
}
get { return _refreshAllowed; }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='FooterElement']/Docs/*" />
[EditorBrowsable(EditorBrowsableState.Never)]
public Element FooterElement
{
get { return _footerElement; }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='HeaderElement']/Docs/*" />
[EditorBrowsable(EditorBrowsableState.Never)]
public Element HeaderElement
{
get { return _headerElement; }
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='SendCellAppearing']/Docs/*" />
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendCellAppearing(Cell cell)
=> ItemAppearing?.Invoke(this, new ItemVisibilityEventArgs(cell.BindingContext, TemplatedItems.GetGlobalIndexOfItem(cell?.BindingContext)));
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='SendCellDisappearing']/Docs/*" />
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendCellDisappearing(Cell cell)
=> ItemDisappearing?.Invoke(this, new ItemVisibilityEventArgs(cell.BindingContext, TemplatedItems.GetGlobalIndexOfItem(cell?.BindingContext)));
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='SendScrolled']/Docs/*" />
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendScrolled(ScrolledEventArgs args)
=> Scrolled?.Invoke(this, args);
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='SendRefreshing']/Docs/*" />
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendRefreshing()
{
BeginRefresh();
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='BeginRefresh']/Docs/*" />
public void BeginRefresh()
{
if (!RefreshAllowed)
return;
SetValue(IsRefreshingProperty, true);
OnRefreshing(EventArgs.Empty);
ICommand command = RefreshCommand;
command?.Execute(null);
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='EndRefresh']/Docs/*" />
public void EndRefresh()
{
SetValue(IsRefreshingProperty, false);
}
public event EventHandler<ItemVisibilityEventArgs> ItemAppearing;
public event EventHandler<ItemVisibilityEventArgs> ItemDisappearing;
public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;
public event EventHandler<ItemTappedEventArgs> ItemTapped;
public event EventHandler<ScrolledEventArgs> Scrolled;
public event EventHandler Refreshing;
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='ScrollTo'][1]/Docs/*" />
public void ScrollTo(object item, ScrollToPosition position, bool animated)
{
if (!Enum.IsDefined(typeof(ScrollToPosition), position))
throw new ArgumentException("position is not a valid ScrollToPosition", "position");
var args = new ScrollToRequestedEventArgs(item, position, animated);
if (IsPlatformEnabled)
OnScrollToRequested(args);
else
_pendingScroll = args;
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='ScrollTo'][2]/Docs/*" />
public void ScrollTo(object item, object group, ScrollToPosition position, bool animated)
{
if (!IsGroupingEnabled)
throw new InvalidOperationException("Grouping is not enabled");
if (!Enum.IsDefined(typeof(ScrollToPosition), position))
throw new ArgumentException("position is not a valid ScrollToPosition", "position");
var args = new ScrollToRequestedEventArgs(item, group, position, animated);
if (IsPlatformEnabled)
OnScrollToRequested(args);
else
_pendingScroll = args;
}
protected override Cell CreateDefault(object item)
{
TextCell textCell = new TextCell();
textCell.SetBinding(
TextCell.TextProperty,
TypedBinding.ForSingleNestingLevel(
propertyName: string.Empty,
getter: static (object cell) => cell,
mode: BindingMode.OneWay,
converter: _toStringValueConverter));
return textCell;
}
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
var minimumSize = new Size(40, 40);
Size request;
var scaled = DeviceDisplay.MainDisplayInfo.GetScaledScreenSize();
double width = Math.Min(scaled.Width, scaled.Height);
var list = ItemsSource as IList;
if (list != null && HasUnevenRows == false && RowHeight > 0 && !IsGroupingEnabled)
{
// we can calculate this
request = new Size(width, list.Count * RowHeight);
}
else
{
// probably not worth it
request = new Size(width, Math.Max(scaled.Width, scaled.Height));
}
return new SizeRequest(request, minimumSize);
}
protected override void SetupContent(Cell content, int index)
{
base.SetupContent(content, index);
if (content is ViewCell viewCell && viewCell.View != null && HasUnevenRows)
viewCell.View.ComputedConstraint = LayoutConstraint.None;
if (content != null)
{
_visualChildren.Add(content);
content.Parent = this;
VisualDiagnostics.OnChildAdded(this, content);
}
}
protected override void UnhookContent(Cell content)
{
base.UnhookContent(content);
if (content == null)
return;
var index = _visualChildren.IndexOf(content);
if (index == -1)
return;
_visualChildren.RemoveAt(index);
content.Parent = null;
VisualDiagnostics.OnChildRemoved(this, content, index);
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='CreateDefaultCell']/Docs/*" />
[EditorBrowsable(EditorBrowsableState.Never)]
public Cell CreateDefaultCell(object item)
{
return CreateDefault(item);
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='GetDisplayTextFromGroup']/Docs/*" />
[EditorBrowsable(EditorBrowsableState.Never)]
public string GetDisplayTextFromGroup(object cell)
{
int groupIndex = TemplatedItems.GetGlobalIndexOfGroup(cell);
if (groupIndex == -1)
return cell.ToString();
var group = TemplatedItems.GetGroup(groupIndex);
string displayBinding = null;
if (GroupDisplayBinding != null)
displayBinding = group.Name;
if (GroupShortNameBinding != null)
displayBinding = group.ShortName;
// TODO: what if they set both? should it default to the ShortName, like it will here?
// ShortNames binding did not appear to be functional before.
return displayBinding;
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='NotifyRowTapped'][2]/Docs/*" />
[EditorBrowsable(EditorBrowsableState.Never)]
public void NotifyRowTapped(int groupIndex, int inGroupIndex, Cell cell = null)
{
var group = TemplatedItems.GetGroup(groupIndex);
bool changed = _previousGroupSelected != groupIndex || _previousRowSelected != inGroupIndex;
_previousRowSelected = inGroupIndex;
_previousGroupSelected = groupIndex;
// A11y: Keyboards and screen readers can deselect items, allowing -1 to be possible
if (cell == null && inGroupIndex >= 0)
{
cell = group[inGroupIndex];
}
// Set SelectedItem before any events so we don't override any changes they may have made.
if (SelectionMode != ListViewSelectionMode.None)
SetValueCore(SelectedItemProperty, cell?.BindingContext, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource | (changed ? SetValueFlags.RaiseOnEqual : 0), SetValuePrivateFlags.Default, SetterSpecificity.FromHandler);
cell?.OnTapped();
ItemTapped?.Invoke(this, new ItemTappedEventArgs(ItemsSource.Cast<object>().ElementAt(groupIndex), cell?.BindingContext, TemplatedItems.GetGlobalIndexOfItem(cell?.BindingContext)));
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='NotifyRowTapped'][4]/Docs/*" />
[EditorBrowsable(EditorBrowsableState.Never)]
public void NotifyRowTapped(int groupIndex, int inGroupIndex, Cell cell, bool isContextMenuRequested)
{
var group = TemplatedItems.GetGroup(groupIndex);
bool changed = _previousGroupSelected != groupIndex || _previousRowSelected != inGroupIndex;
_previousRowSelected = inGroupIndex;
_previousGroupSelected = groupIndex;
// A11y: Keyboards and screen readers can deselect items, allowing -1 to be possible
if (cell == null && inGroupIndex >= 0 && group.Count > inGroupIndex)
{
cell = group[inGroupIndex];
}
// Set SelectedItem before any events so we don't override any changes they may have made.
if (SelectionMode != ListViewSelectionMode.None)
SetValueCore(SelectedItemProperty, cell?.BindingContext, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource | (changed ? SetValueFlags.RaiseOnEqual : 0), SetValuePrivateFlags.Default, SetterSpecificity.FromHandler);
if (isContextMenuRequested || cell == null)
{
return;
}
cell.OnTapped();
var itemSource = ItemsSource?.Cast<object>().ToList();
object tappedGroup = null;
if (itemSource?.Count > groupIndex)
{
tappedGroup = itemSource.ElementAt(groupIndex);
}
ItemTapped?.Invoke(this,
new ItemTappedEventArgs(tappedGroup, cell.BindingContext,
TemplatedItems.GetGlobalIndexOfItem(cell?.BindingContext)));
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='NotifyRowTapped'][1]/Docs/*" />
[EditorBrowsable(EditorBrowsableState.Never)]
public void NotifyRowTapped(int index, Cell cell = null)
{
if (IsGroupingEnabled)
{
int leftOver;
int groupIndex = TemplatedItems.GetGroupIndexFromGlobal(index, out leftOver);
NotifyRowTapped(groupIndex, leftOver - 1, cell);
}
else
NotifyRowTapped(0, index, cell);
}
/// <include file="../../docs/Microsoft.Maui.Controls/ListView.xml" path="//Member[@MemberName='NotifyRowTapped'][3]/Docs/*" />
[EditorBrowsable(EditorBrowsableState.Never)]
public void NotifyRowTapped(int index, Cell cell, bool isContextmenuRequested)
{
if (IsGroupingEnabled)
{
int leftOver;
int groupIndex = TemplatedItems.GetGroupIndexFromGlobal(index, out leftOver);
NotifyRowTapped(groupIndex, leftOver - 1, cell, isContextmenuRequested);
}
else
NotifyRowTapped(0, index, cell, isContextmenuRequested);
}
internal override void OnIsPlatformEnabledChanged()
{
base.OnIsPlatformEnabledChanged();
if (IsPlatformEnabled && _pendingScroll != null)
{
OnScrollToRequested(_pendingScroll);
_pendingScroll = null;
}
}
[EditorBrowsable(EditorBrowsableState.Never)]
public event EventHandler<ScrollToRequestedEventArgs> ScrollToRequested;
void OnCommandCanExecuteChanged(object sender, EventArgs eventArgs)
{
RefreshAllowed = RefreshCommand != null && RefreshCommand.CanExecute(null);
}
static void OnFooterChanged(BindableObject bindable, object oldValue, object newValue)
{
var lv = (ListView)bindable;
lv.OnHeaderOrFooterChanged(ref lv._footerElement, "FooterElement", newValue, lv.FooterTemplate, false);
}
static void OnFooterTemplateChanged(BindableObject bindable, object oldValue, object newValue)
{
var lv = (ListView)bindable;
lv.OnHeaderOrFooterChanged(ref lv._footerElement, "FooterElement", lv.Footer, (DataTemplate)newValue, true);
}
static void OnGroupDisplayBindingChanged(BindableObject bindable, BindingBase oldValue, BindingBase newValue)
{
var lv = (ListView)bindable;
if (newValue != null && lv.GroupHeaderTemplate != null)
{
lv.GroupHeaderTemplate = null;
Application.Current?.FindMauiContext()?.CreateLogger<ListView>()?.LogWarning("GroupHeaderTemplate and GroupDisplayBinding cannot be set at the same time, setting GroupHeaderTemplate to null");
}
}
static void OnGroupHeaderTemplateChanged(BindableObject bindable, object oldvalue, object newValue)
{
var lv = (ListView)bindable;
if (newValue != null && lv.GroupDisplayBinding != null)
{
lv.GroupDisplayBinding = null;
Application.Current?.FindMauiContext()?.CreateLogger<ListView>()?.LogWarning("GroupHeaderTemplate and GroupDisplayBinding cannot be set at the same time, setting GroupDisplayBinding to null");
}
}
static void OnHeaderChanged(BindableObject bindable, object oldValue, object newValue)
{
var lv = (ListView)bindable;
lv.OnHeaderOrFooterChanged(ref lv._headerElement, "HeaderElement", newValue, lv.HeaderTemplate, false);
}
void OnHeaderOrFooterChanged(ref Element storage, string property, object dataObject, DataTemplate template, bool templateChanged)
{
if (dataObject == null)
{
if (!templateChanged)
{
OnPropertyChanging(property);
storage = null;
OnPropertyChanged(property);
}
return;
}
if (template == null)
{
var view = dataObject as Element;
if (view == null || view is Page)
view = new Label { Text = dataObject.ToString() };
view.Parent = this;
OnPropertyChanging(property);
storage = view;
OnPropertyChanged(property);
}
else if (storage == null || templateChanged)
{
OnPropertyChanging(property);
storage = template.CreateContent() as Element;
if (storage != null)
{
storage.BindingContext = dataObject;
storage.Parent = this;
}
OnPropertyChanged(property);
}
else
{
storage.BindingContext = dataObject;
}
}
static void OnHeaderTemplateChanged(BindableObject bindable, object oldValue, object newValue)
{
var lv = (ListView)bindable;
lv.OnHeaderOrFooterChanged(ref lv._headerElement, "HeaderElement", lv.Header, (DataTemplate)newValue, true);
}
static void OnRefreshCommandChanged(BindableObject bindable, object oldValue, object newValue)
{
var lv = (ListView)bindable;
var oldCommand = (ICommand)oldValue;
var command = (ICommand)newValue;
lv.OnRefreshCommandChanged(oldCommand, command);
}
void OnRefreshCommandChanged(ICommand oldCommand, ICommand newCommand)
{
if (oldCommand != null)
{
oldCommand.CanExecuteChanged -= OnCommandCanExecuteChanged;
}
if (newCommand != null)
{
newCommand.CanExecuteChanged += OnCommandCanExecuteChanged;
RefreshAllowed = newCommand.CanExecute(null);
}
else
{
RefreshAllowed = true;
}
}
void OnRefreshing(EventArgs e)
=> Refreshing?.Invoke(this, e);
void OnScrollToRequested(ScrollToRequestedEventArgs e)
=> ScrollToRequested?.Invoke(this, e);
static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
=> ((ListView)bindable).ItemSelected?.Invoke(bindable, new SelectedItemChangedEventArgs(newValue, ((ListView)bindable).TemplatedItems.GetGlobalIndexOfItem(newValue)));
static bool ValidateHeaderFooterTemplate(BindableObject bindable, object value)
{
if (value == null)
return true;
var template = (DataTemplate)value;
return template.CreateContent() is View;
}
/// <inheritdoc/>
public IPlatformElementConfiguration<T, ListView> On<T>() where T : IConfigPlatform
{
return _platformConfigurationRegistry.Value.On<T>();
}
protected override bool ValidateItemTemplate(DataTemplate template)
{
var isRetainStrategy = CachingStrategy == ListViewCachingStrategy.RetainElement;
var isDataTemplateSelector = ItemTemplate is DataTemplateSelector;
if (isRetainStrategy && isDataTemplateSelector)
return false;
return true;
}
}
}