2022-09-09 20:58:11 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
2019-05-08 22:37:58 +02:00
|
|
|
|
using System.Collections.Specialized;
|
2019-04-25 17:43:45 +02:00
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Bit.App.Controls
|
|
|
|
|
{
|
2022-09-09 20:58:11 +02:00
|
|
|
|
[Obsolete]
|
2019-04-25 17:43:45 +02:00
|
|
|
|
public class RepeaterView : StackLayout
|
|
|
|
|
{
|
|
|
|
|
public static readonly BindableProperty ItemTemplateProperty = BindableProperty.Create(
|
|
|
|
|
nameof(ItemTemplate), typeof(DataTemplate), typeof(RepeaterView), default(DataTemplate));
|
|
|
|
|
|
|
|
|
|
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
|
|
|
|
|
nameof(ItemsSource), typeof(ICollection), typeof(RepeaterView), null, BindingMode.OneWay,
|
2019-05-08 22:37:58 +02:00
|
|
|
|
propertyChanged: ItemsSourceChanging);
|
2019-04-25 17:43:45 +02:00
|
|
|
|
|
|
|
|
|
public RepeaterView()
|
|
|
|
|
{
|
|
|
|
|
Spacing = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ICollection ItemsSource
|
|
|
|
|
{
|
|
|
|
|
get => GetValue(ItemsSourceProperty) as ICollection;
|
|
|
|
|
set => SetValue(ItemsSourceProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DataTemplate ItemTemplate
|
|
|
|
|
{
|
|
|
|
|
get => GetValue(ItemTemplateProperty) as DataTemplate;
|
|
|
|
|
set => SetValue(ItemTemplateProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 22:37:58 +02:00
|
|
|
|
private void OnCollectionChanged(object sender,
|
|
|
|
|
NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
|
|
|
|
|
{
|
|
|
|
|
Populate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnPropertyChanged(string propertyName = null)
|
|
|
|
|
{
|
|
|
|
|
base.OnPropertyChanged(propertyName);
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (propertyName == ItemTemplateProperty.PropertyName || propertyName == ItemsSourceProperty.PropertyName)
|
2019-05-08 22:37:58 +02:00
|
|
|
|
{
|
|
|
|
|
Populate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnBindingContextChanged()
|
|
|
|
|
{
|
|
|
|
|
base.OnBindingContextChanged();
|
|
|
|
|
Populate();
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 17:43:45 +02:00
|
|
|
|
protected virtual View ViewFor(object item)
|
|
|
|
|
{
|
|
|
|
|
View view = null;
|
|
|
|
|
var template = ItemTemplate;
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (template != null)
|
2019-04-25 17:43:45 +02:00
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (template is DataTemplateSelector selector)
|
2019-04-25 17:43:45 +02:00
|
|
|
|
{
|
|
|
|
|
template = selector.SelectTemplate(item, this);
|
|
|
|
|
}
|
|
|
|
|
var content = template.CreateContent();
|
|
|
|
|
view = content is View ? content as View : ((ViewCell)content).View;
|
|
|
|
|
view.BindingContext = item;
|
|
|
|
|
}
|
|
|
|
|
return view;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 22:37:58 +02:00
|
|
|
|
private void Populate()
|
2019-04-25 17:43:45 +02:00
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (ItemsSource != null)
|
2019-04-25 17:43:45 +02:00
|
|
|
|
{
|
2019-05-08 22:37:58 +02:00
|
|
|
|
Children.Clear();
|
2020-03-28 14:16:28 +01:00
|
|
|
|
foreach (var item in ItemsSource)
|
2019-04-25 17:43:45 +02:00
|
|
|
|
{
|
2019-05-08 22:37:58 +02:00
|
|
|
|
Children.Add(ViewFor(item));
|
2019-04-25 17:43:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-08 22:37:58 +02:00
|
|
|
|
|
|
|
|
|
private static void ItemsSourceChanging(BindableObject bindable, object oldValue, object newValue)
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (oldValue != null && oldValue is INotifyCollectionChanged ov)
|
2019-05-08 22:37:58 +02:00
|
|
|
|
{
|
|
|
|
|
ov.CollectionChanged -= (bindable as RepeaterView).OnCollectionChanged;
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (newValue != null && newValue is INotifyCollectionChanged nv)
|
2019-05-08 22:37:58 +02:00
|
|
|
|
{
|
|
|
|
|
nv.CollectionChanged += (bindable as RepeaterView).OnCollectionChanged;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-25 17:43:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|