a picker behavior

Signed-off-by: Paul Schneider <paul@pschneider.fr>
This commit is contained in:
2016-10-07 14:27:14 +02:00
parent a6f0daf0a8
commit bbdb7adc67

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace BookAStar.Behaviors
{
public class PickerBehavior : Behavior<Picker>
{
// Creating BindableProperties with Limited write access: http://iosapi.xamarin.com/index.aspx?link=M%3AXamarin.Forms.BindableObject.SetValue(Xamarin.Forms.BindablePropertyKey%2CSystem.Object)
public static readonly BindablePropertyKey SelectedItemPropertyKey = BindableProperty.CreateReadOnly("SelectedItem", typeof(string), typeof(PickerBehavior), default(string));
public static readonly BindableProperty SelectedItemProperty = SelectedItemPropertyKey.BindableProperty;
public string SelectedItem
{
get { return (string)base.GetValue(SelectedItemProperty); }
private set { base.SetValue(SelectedItemPropertyKey, value); }
}
protected override void OnAttachedTo(Picker bindable)
{
bindable.SelectedIndexChanged += bindable_SelectedIndexChanged;
}
void bindable_SelectedIndexChanged(object sender, EventArgs e)
{
var picker = (Picker)sender;
if (picker.SelectedIndex <= 0 || picker.SelectedIndex > picker.Items.Count - 1)
SelectedItem = null;
else
SelectedItem=picker.Items[picker.SelectedIndex];
}
protected override void OnDetachingFrom(Picker bindable)
{
bindable.SelectedIndexChanged -= bindable_SelectedIndexChanged;
}
}
}