1 using System.Collections.Generic;
4 namespace Xamarin.Forms.Pages
6 public class DataView : ContentView, IDataSourceProvider
8 public static readonly BindableProperty DataProperty = BindableProperty.Create(nameof(Data), typeof(IEnumerable<IDataItem>), typeof(DataView), default(IEnumerable<IDataItem>));
10 public static readonly BindableProperty DataSourceProperty = BindableProperty.Create(nameof(DataSource), typeof(IDataSource), typeof(DataView), null, propertyChanged: OnDataSourceChanged);
12 public static readonly BindableProperty DefaultItemTemplateProperty = BindableProperty.Create(nameof(DefaultItemTemplate), typeof(DataTemplate), typeof(DataView), default(DataTemplate));
14 readonly HashSet<string> _maskedKeys = new HashSet<string>();
18 SetBinding(DataProperty, new Binding("DataSource.Data", source: this));
21 public IEnumerable<IDataItem> Data
23 get { return (IEnumerable<IDataItem>)GetValue(DataProperty); }
24 set { SetValue(DataProperty, value); }
27 public DataTemplate DefaultItemTemplate
29 get { return (DataTemplate)GetValue(DefaultItemTemplateProperty); }
30 set { SetValue(DefaultItemTemplateProperty, value); }
33 public IDataSource DataSource
35 get { return (IDataSource)GetValue(DataSourceProperty); }
36 set { SetValue(DataSourceProperty, value); }
39 void IDataSourceProvider.MaskKey(string key)
42 IDataSource dataSource = DataSource;
43 if (dataSource != null && !dataSource.MaskedKeys.Contains(key))
45 dataSource.MaskKey(key);
49 void IDataSourceProvider.UnmaskKey(string key)
51 _maskedKeys.Remove(key);
52 DataSource?.UnmaskKey(key);
55 static void OnDataSourceChanged(BindableObject bindable, object oldValue, object newValue)
57 var dataView = (DataView)bindable;
58 var dataSource = (IDataSource)newValue;
59 var oldSource = (IDataSource)oldValue;
61 if (oldSource != null)
63 foreach (string key in dataView._maskedKeys)
64 oldSource.UnmaskKey(key);
67 if (dataSource != null)
69 foreach (string key in dataView._maskedKeys)
71 dataSource.MaskKey(key);