2016-03-22 23:02:25 +03:00
|
|
|
using System;
|
2016-11-09 11:23:52 +03:00
|
|
|
using System.Collections;
|
2016-03-22 23:02:25 +03:00
|
|
|
using System.Collections.Generic;
|
2017-01-27 12:16:51 +03:00
|
|
|
using System.Collections.ObjectModel;
|
2016-03-22 23:02:25 +03:00
|
|
|
using System.Collections.Specialized;
|
2017-01-27 12:16:51 +03:00
|
|
|
using System.ComponentModel;
|
|
|
|
using System.Runtime.CompilerServices;
|
2017-03-07 22:56:24 +03:00
|
|
|
using Xamarin.Forms.Internals;
|
2016-03-22 23:02:25 +03:00
|
|
|
using Xamarin.Forms.Platform;
|
|
|
|
|
|
|
|
namespace Xamarin.Forms
|
|
|
|
{
|
|
|
|
[RenderWith(typeof(_PickerRenderer))]
|
2017-12-12 17:02:21 +03:00
|
|
|
public class Picker : View, IFontElement, ITextElement, IElementConfiguration<Picker>
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2017-03-06 12:35:17 +03:00
|
|
|
public static readonly BindableProperty TextColorProperty = TextElement.TextColorProperty;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
public static readonly BindableProperty TitleProperty =
|
|
|
|
BindableProperty.Create(nameof(Title), typeof(string), typeof(Picker), default(string));
|
2016-04-19 10:01:58 +03:00
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
public static readonly BindableProperty SelectedIndexProperty =
|
|
|
|
BindableProperty.Create(nameof(SelectedIndex), typeof(int), typeof(Picker), -1, BindingMode.TwoWay,
|
|
|
|
propertyChanged: OnSelectedIndexChanged, coerceValue: CoerceSelectedIndex);
|
2016-11-09 11:23:52 +03:00
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
public static readonly BindableProperty ItemsSourceProperty =
|
|
|
|
BindableProperty.Create(nameof(ItemsSource), typeof(IList), typeof(Picker), default(IList),
|
|
|
|
propertyChanged: OnItemsSourceChanged);
|
2016-11-09 11:23:52 +03:00
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
public static readonly BindableProperty SelectedItemProperty =
|
|
|
|
BindableProperty.Create(nameof(SelectedItem), typeof(object), typeof(Picker), null, BindingMode.TwoWay,
|
|
|
|
propertyChanged: OnSelectedItemChanged);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2017-12-12 17:02:21 +03:00
|
|
|
public static readonly BindableProperty FontFamilyProperty = FontElement.FontFamilyProperty;
|
|
|
|
|
|
|
|
public static readonly BindableProperty FontSizeProperty = FontElement.FontSizeProperty;
|
|
|
|
|
|
|
|
public static readonly BindableProperty FontAttributesProperty = FontElement.FontAttributesProperty;
|
|
|
|
|
2016-08-30 20:46:14 +03:00
|
|
|
readonly Lazy<PlatformConfigurationRegistry<Picker>> _platformConfigurationRegistry;
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
public Picker()
|
|
|
|
{
|
2016-11-09 17:15:12 +03:00
|
|
|
((INotifyCollectionChanged)Items).CollectionChanged += OnItemsCollectionChanged;
|
2016-08-30 20:46:14 +03:00
|
|
|
_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<Picker>>(() => new PlatformConfigurationRegistry<Picker>(this));
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
2017-12-12 17:02:21 +03:00
|
|
|
public FontAttributes FontAttributes
|
|
|
|
{
|
|
|
|
get { return (FontAttributes)GetValue(FontAttributesProperty); }
|
|
|
|
set { SetValue(FontAttributesProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public string FontFamily
|
|
|
|
{
|
|
|
|
get { return (string)GetValue(FontFamilyProperty); }
|
|
|
|
set { SetValue(FontFamilyProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
[TypeConverter(typeof(FontSizeConverter))]
|
|
|
|
public double FontSize
|
|
|
|
{
|
|
|
|
get { return (double)GetValue(FontSizeProperty); }
|
|
|
|
set { SetValue(FontSizeProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
void IFontElement.OnFontFamilyChanged(string oldValue, string newValue) =>
|
|
|
|
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
|
|
|
|
|
|
|
|
void IFontElement.OnFontSizeChanged(double oldValue, double newValue) =>
|
|
|
|
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
|
|
|
|
|
|
|
|
void IFontElement.OnFontChanged(Font oldValue, Font newValue) =>
|
|
|
|
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
|
|
|
|
|
|
|
|
double IFontElement.FontSizeDefaultValueCreator() =>
|
|
|
|
Device.GetNamedSize(NamedSize.Default, (Picker)this);
|
|
|
|
|
|
|
|
void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) =>
|
|
|
|
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
public IList<string> Items { get; } = new LockableObservableListWrapper();
|
2016-11-09 11:23:52 +03:00
|
|
|
|
|
|
|
public IList ItemsSource
|
|
|
|
{
|
|
|
|
get { return (IList)GetValue(ItemsSourceProperty); }
|
|
|
|
set { SetValue(ItemsSourceProperty, value); }
|
|
|
|
}
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
public int SelectedIndex
|
|
|
|
{
|
|
|
|
get { return (int)GetValue(SelectedIndexProperty); }
|
|
|
|
set { SetValue(SelectedIndexProperty, value); }
|
|
|
|
}
|
|
|
|
|
2016-11-09 11:23:52 +03:00
|
|
|
public object SelectedItem
|
|
|
|
{
|
|
|
|
get { return GetValue(SelectedItemProperty); }
|
|
|
|
set { SetValue(SelectedItemProperty, value); }
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
public Color TextColor {
|
2017-03-06 12:35:17 +03:00
|
|
|
get { return (Color)GetValue(TextElement.TextColorProperty); }
|
|
|
|
set { SetValue(TextElement.TextColorProperty, value); }
|
2016-04-19 10:01:58 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
public string Title {
|
2016-03-22 23:02:25 +03:00
|
|
|
get { return (string)GetValue(TitleProperty); }
|
|
|
|
set { SetValue(TitleProperty, value); }
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
BindingBase _itemDisplayBinding;
|
|
|
|
public BindingBase ItemDisplayBinding {
|
|
|
|
get { return _itemDisplayBinding; }
|
|
|
|
set {
|
|
|
|
if (_itemDisplayBinding == value)
|
|
|
|
return;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
OnPropertyChanging();
|
|
|
|
var oldValue = value;
|
|
|
|
_itemDisplayBinding = value;
|
|
|
|
OnItemDisplayBindingChanged(oldValue, _itemDisplayBinding);
|
|
|
|
OnPropertyChanged();
|
2016-11-09 11:23:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
public event EventHandler SelectedIndexChanged;
|
|
|
|
|
|
|
|
static readonly BindableProperty s_displayProperty =
|
|
|
|
BindableProperty.Create("Display", typeof(string), typeof(Picker), default(string));
|
2016-11-09 11:23:52 +03:00
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
string GetDisplayMember(object item)
|
2016-11-09 11:23:52 +03:00
|
|
|
{
|
2016-11-09 17:15:12 +03:00
|
|
|
if (ItemDisplayBinding == null)
|
|
|
|
return item.ToString();
|
|
|
|
|
|
|
|
ItemDisplayBinding.Apply(item, this, s_displayProperty);
|
|
|
|
ItemDisplayBinding.Unapply();
|
|
|
|
return (string)GetValue(s_displayProperty);
|
2016-11-09 11:23:52 +03:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
static object CoerceSelectedIndex(BindableObject bindable, object value)
|
|
|
|
{
|
|
|
|
var picker = (Picker)bindable;
|
|
|
|
return picker.Items == null ? -1 : ((int)value).Clamp(-1, picker.Items.Count - 1);
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
void OnItemDisplayBindingChanged(BindingBase oldValue, BindingBase newValue)
|
2016-11-09 11:23:52 +03:00
|
|
|
{
|
2016-11-09 17:15:12 +03:00
|
|
|
ResetItems();
|
2016-11-09 11:23:52 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
2016-11-09 11:23:52 +03:00
|
|
|
{
|
2016-11-09 17:15:12 +03:00
|
|
|
SelectedIndex = SelectedIndex.Clamp(-1, Items.Count - 1);
|
|
|
|
UpdateSelectedItem();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
|
|
|
|
{
|
|
|
|
((Picker)bindable).OnItemsSourceChanged((IList)oldValue, (IList)newValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnItemsSourceChanged(IList oldValue, IList newValue)
|
|
|
|
{
|
|
|
|
var oldObservable = oldValue as INotifyCollectionChanged;
|
|
|
|
if (oldObservable != null)
|
|
|
|
oldObservable.CollectionChanged -= CollectionChanged;
|
|
|
|
|
|
|
|
var newObservable = newValue as INotifyCollectionChanged;
|
|
|
|
if (newObservable != null) {
|
|
|
|
newObservable.CollectionChanged += CollectionChanged;
|
2016-11-09 11:23:52 +03:00
|
|
|
}
|
2016-11-09 17:15:12 +03:00
|
|
|
|
|
|
|
if (newValue != null) {
|
|
|
|
((LockableObservableListWrapper)Items).IsLocked = true;
|
|
|
|
ResetItems();
|
|
|
|
} else {
|
|
|
|
((LockableObservableListWrapper)Items).InternalClear();
|
|
|
|
((LockableObservableListWrapper)Items).IsLocked = false;
|
2016-11-09 11:23:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
2016-11-09 11:23:52 +03:00
|
|
|
{
|
2016-11-09 17:15:12 +03:00
|
|
|
switch (e.Action) {
|
|
|
|
case NotifyCollectionChangedAction.Add:
|
|
|
|
AddItems(e);
|
|
|
|
break;
|
|
|
|
case NotifyCollectionChangedAction.Remove:
|
|
|
|
RemoveItems(e);
|
|
|
|
break;
|
|
|
|
default: //Move, Replace, Reset
|
|
|
|
ResetItems();
|
|
|
|
break;
|
2016-11-09 11:23:52 +03:00
|
|
|
}
|
|
|
|
}
|
2016-11-09 17:15:12 +03:00
|
|
|
void AddItems(NotifyCollectionChangedEventArgs e)
|
|
|
|
{
|
|
|
|
int index = e.NewStartingIndex < 0 ? Items.Count : e.NewStartingIndex;
|
|
|
|
foreach (object newItem in e.NewItems)
|
|
|
|
((LockableObservableListWrapper)Items).InternalInsert(index++, GetDisplayMember(newItem));
|
|
|
|
}
|
2016-11-09 11:23:52 +03:00
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
void RemoveItems(NotifyCollectionChangedEventArgs e)
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2016-11-09 17:15:12 +03:00
|
|
|
int index = e.OldStartingIndex < Items.Count ? e.OldStartingIndex : Items.Count;
|
|
|
|
foreach (object _ in e.OldItems)
|
|
|
|
((LockableObservableListWrapper)Items).InternalRemoveAt(index--);
|
2016-11-09 11:23:52 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 17:15:12 +03:00
|
|
|
void ResetItems()
|
2016-11-09 11:23:52 +03:00
|
|
|
{
|
2016-11-09 17:15:12 +03:00
|
|
|
if (ItemsSource == null)
|
|
|
|
return;
|
|
|
|
((LockableObservableListWrapper)Items).InternalClear();
|
|
|
|
foreach (object item in ItemsSource)
|
|
|
|
((LockableObservableListWrapper)Items).InternalAdd(GetDisplayMember(item));
|
|
|
|
UpdateSelectedItem();
|
2016-11-09 11:23:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void OnSelectedIndexChanged(object bindable, object oldValue, object newValue)
|
|
|
|
{
|
|
|
|
var picker = (Picker)bindable;
|
|
|
|
picker.UpdateSelectedItem();
|
2017-02-01 19:52:40 +03:00
|
|
|
picker.SelectedIndexChanged?.Invoke(bindable, EventArgs.Empty);
|
2016-11-09 11:23:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
|
|
|
|
{
|
|
|
|
var picker = (Picker)bindable;
|
|
|
|
picker.UpdateSelectedIndex(newValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateSelectedIndex(object selectedItem)
|
|
|
|
{
|
2016-11-09 17:15:12 +03:00
|
|
|
if (ItemsSource != null) {
|
|
|
|
SelectedIndex = ItemsSource.IndexOf(selectedItem);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SelectedIndex = Items.IndexOf(selectedItem);
|
2016-11-09 11:23:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateSelectedItem()
|
|
|
|
{
|
2016-11-09 17:15:12 +03:00
|
|
|
if (SelectedIndex == -1) {
|
|
|
|
SelectedItem = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ItemsSource != null) {
|
|
|
|
SelectedItem = ItemsSource [SelectedIndex];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectedItem = Items [SelectedIndex];
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
2016-08-30 20:46:14 +03:00
|
|
|
|
|
|
|
public IPlatformElementConfiguration<T, Picker> On<T>() where T : IConfigPlatform
|
|
|
|
{
|
|
|
|
return _platformConfigurationRegistry.Value.On<T>();
|
|
|
|
}
|
2016-11-09 17:15:12 +03:00
|
|
|
|
2017-03-06 12:35:17 +03:00
|
|
|
void ITextElement.OnTextColorPropertyChanged(Color oldValue, Color newValue)
|
|
|
|
{
|
|
|
|
}
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
2016-11-09 17:15:12 +03:00
|
|
|
}
|