BindablePicker em Xamarin
05/11/2019
0
Estou com uma dúvida relacionada a BindablePicker em Xamarin, gostaria de saber se existe a possibilidade de ser realizada a seguinte ação: ao clicar sobre o BindablePicker ao invés de aparecer uma lista com meus clientes por exemplo, gostaria de aparece uma nova page e nela contiver uma busca para esses clientes e ao realizar essa busca e selecionar o cliente que desejo, ele ao ser clicado deve ser redirecionado para a página anterior onde ficaria registrado o nome desse cliente. Gostaria de saber se existe esse possibilidade. Abaixo está meu código do BindablePicker. Ficarei muito agradecida se puder sanar essa minha dúvida. Ou então também qual seria a possibilidade de criar uma busca dentro do próprio BindablePicker, ou ainda se seria possível criar um autocomplete no BindablePicker ?
<Label
Text="Cliente:"
FontSize="18"
TextColor="{StaticResource FontColor}"
VerticalOptions="Center"
HorizontalOptions="Start">
</Label>
<control:BindablePicker
x:Name="Clientes"
Title=""
FontSize="18"
SelectedIndexChanged="pckSexo_SelectedIndexChanged"
DisplayMemberPath="NomeCliente"
SelectedValuePath="IdCliente"
ItemsSource="{Binding CopalCliente}"
SelectedValue ="{Binding Path=IdCliente, Mode=TwoWay}"
HorizontalOptions="FillAndExpand"
VerticalOptions="Center">
</control:BindablePicker>
Código do BindablePicker
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using Xamarin.Forms;
namespace ECommerceAPP.Controls
{
public class BindablePicker : Picker
{
Boolean _disableNestedCalls;
public new static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(BindablePicker),
null, propertyChanged: OnItemsSourceChanged);
public new static readonly BindableProperty SelectedItemProperty =
BindableProperty.Create("SelectedItem", typeof(Object), typeof(BindablePicker),
null, BindingMode.TwoWay, propertyChanged: OnSelectedItemChanged);
public static readonly BindableProperty SelectedValueProperty =
BindableProperty.Create("SelectedValue", typeof(Object), typeof(BindablePicker),
null, BindingMode.TwoWay, propertyChanged: OnSelectedValueChanged);
public String DisplayMemberPath { get; set; }
public new IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public new Object SelectedItem
{
get { return GetValue(SelectedItemProperty); }
set
{
if (this.SelectedItem != value)
{
SetValue(SelectedItemProperty, value);
InternalSelectedItemChanged();
}
}
}
public Object SelectedValue
{
get { return GetValue(SelectedValueProperty); }
set
{
SetValue(SelectedValueProperty, value);
InternalSelectedValueChanged();
}
}
public String SelectedValuePath { get; set; }
public BindablePicker()
{
this.SelectedIndexChanged += OnSelectedIndexChanged;
}
public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;
void InstanceOnItemsSourceChanged(Object oldValue, Object newValue)
{
_disableNestedCalls = true;
this.Items.Clear();
var oldCollectionINotifyCollectionChanged = oldValue as INotifyCollectionChanged;
if (oldCollectionINotifyCollectionChanged != null)
{
oldCollectionINotifyCollectionChanged.CollectionChanged -= ItemsSource_CollectionChanged;
}
var newCollectionINotifyCollectionChanged = newValue as INotifyCollectionChanged;
if (newCollectionINotifyCollectionChanged != null)
{
newCollectionINotifyCollectionChanged.CollectionChanged += ItemsSource_CollectionChanged;
}
if (!Equals(newValue, null))
{
var hasDisplayMemberPath = !String.IsNullOrWhiteSpace(this.DisplayMemberPath);
foreach (var item in (IEnumerable)newValue)
{
if (hasDisplayMemberPath)
{
var type = item.GetType();
var prop = type.GetRuntimeProperty(this.DisplayMemberPath);
this.Items.Add(prop.GetValue(item).ToString());
}
else
{
this.Items.Add(item.ToString());
}
}
this.SelectedIndex = -1;
this._disableNestedCalls = false;
if (this.SelectedItem != null)
{
this.InternalSelectedItemChanged();
}
else if (hasDisplayMemberPath && this.SelectedValue != null)
{
this.InternalSelectedValueChanged();
}
}
else
{
_disableNestedCalls = true;
this.SelectedIndex = -1;
this.SelectedItem = null;
this.SelectedValue = null;
_disableNestedCalls = false;
}
}
void InternalSelectedItemChanged()
{
if (_disableNestedCalls)
{
return;
}
var selectedIndex = -1;
Object selectedValue = null;
if (this.ItemsSource != null)
{
var index = 0;
var hasSelectedValuePath = !String.IsNullOrWhiteSpace(this.SelectedValuePath);
foreach (var item in this.ItemsSource)
{
if (item != null && item.Equals(this.SelectedItem))
{
selectedIndex = index;
if (hasSelectedValuePath)
{
var type = item.GetType();
var prop = type.GetRuntimeProperty(this.SelectedValuePath);
selectedValue = prop.GetValue(item);
}
break;
}
index++;
}
}
_disableNestedCalls = true;
this.SelectedValue = selectedValue;
this.SelectedIndex = selectedIndex;
_disableNestedCalls = false;
}
void InternalSelectedValueChanged()
{
if (_disableNestedCalls)
{
return;
}
if (String.IsNullOrWhiteSpace(this.SelectedValuePath))
{
return;
}
var selectedIndex = -1;
Object selectedItem = null;
var hasSelectedValuePath = !String.IsNullOrWhiteSpace(this.SelectedValuePath);
if (this.ItemsSource != null && hasSelectedValuePath)
{
var index = 0;
foreach (var item in this.ItemsSource)
{
if (item != null)
{
var type = item.GetType();
var prop = type.GetRuntimeProperty(this.SelectedValuePath);
if (prop.GetValue(item) == this.SelectedValue)
{
selectedIndex = index;
selectedItem = item;
<Label
Text="Cliente:"
FontSize="18"
TextColor="{StaticResource FontColor}"
VerticalOptions="Center"
HorizontalOptions="Start">
</Label>
<control:BindablePicker
x:Name="Clientes"
Title=""
FontSize="18"
SelectedIndexChanged="pckSexo_SelectedIndexChanged"
DisplayMemberPath="NomeCliente"
SelectedValuePath="IdCliente"
ItemsSource="{Binding CopalCliente}"
SelectedValue ="{Binding Path=IdCliente, Mode=TwoWay}"
HorizontalOptions="FillAndExpand"
VerticalOptions="Center">
</control:BindablePicker>
Código do BindablePicker
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using Xamarin.Forms;
namespace ECommerceAPP.Controls
{
public class BindablePicker : Picker
{
Boolean _disableNestedCalls;
public new static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(BindablePicker),
null, propertyChanged: OnItemsSourceChanged);
public new static readonly BindableProperty SelectedItemProperty =
BindableProperty.Create("SelectedItem", typeof(Object), typeof(BindablePicker),
null, BindingMode.TwoWay, propertyChanged: OnSelectedItemChanged);
public static readonly BindableProperty SelectedValueProperty =
BindableProperty.Create("SelectedValue", typeof(Object), typeof(BindablePicker),
null, BindingMode.TwoWay, propertyChanged: OnSelectedValueChanged);
public String DisplayMemberPath { get; set; }
public new IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public new Object SelectedItem
{
get { return GetValue(SelectedItemProperty); }
set
{
if (this.SelectedItem != value)
{
SetValue(SelectedItemProperty, value);
InternalSelectedItemChanged();
}
}
}
public Object SelectedValue
{
get { return GetValue(SelectedValueProperty); }
set
{
SetValue(SelectedValueProperty, value);
InternalSelectedValueChanged();
}
}
public String SelectedValuePath { get; set; }
public BindablePicker()
{
this.SelectedIndexChanged += OnSelectedIndexChanged;
}
public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;
void InstanceOnItemsSourceChanged(Object oldValue, Object newValue)
{
_disableNestedCalls = true;
this.Items.Clear();
var oldCollectionINotifyCollectionChanged = oldValue as INotifyCollectionChanged;
if (oldCollectionINotifyCollectionChanged != null)
{
oldCollectionINotifyCollectionChanged.CollectionChanged -= ItemsSource_CollectionChanged;
}
var newCollectionINotifyCollectionChanged = newValue as INotifyCollectionChanged;
if (newCollectionINotifyCollectionChanged != null)
{
newCollectionINotifyCollectionChanged.CollectionChanged += ItemsSource_CollectionChanged;
}
if (!Equals(newValue, null))
{
var hasDisplayMemberPath = !String.IsNullOrWhiteSpace(this.DisplayMemberPath);
foreach (var item in (IEnumerable)newValue)
{
if (hasDisplayMemberPath)
{
var type = item.GetType();
var prop = type.GetRuntimeProperty(this.DisplayMemberPath);
this.Items.Add(prop.GetValue(item).ToString());
}
else
{
this.Items.Add(item.ToString());
}
}
this.SelectedIndex = -1;
this._disableNestedCalls = false;
if (this.SelectedItem != null)
{
this.InternalSelectedItemChanged();
}
else if (hasDisplayMemberPath && this.SelectedValue != null)
{
this.InternalSelectedValueChanged();
}
}
else
{
_disableNestedCalls = true;
this.SelectedIndex = -1;
this.SelectedItem = null;
this.SelectedValue = null;
_disableNestedCalls = false;
}
}
void InternalSelectedItemChanged()
{
if (_disableNestedCalls)
{
return;
}
var selectedIndex = -1;
Object selectedValue = null;
if (this.ItemsSource != null)
{
var index = 0;
var hasSelectedValuePath = !String.IsNullOrWhiteSpace(this.SelectedValuePath);
foreach (var item in this.ItemsSource)
{
if (item != null && item.Equals(this.SelectedItem))
{
selectedIndex = index;
if (hasSelectedValuePath)
{
var type = item.GetType();
var prop = type.GetRuntimeProperty(this.SelectedValuePath);
selectedValue = prop.GetValue(item);
}
break;
}
index++;
}
}
_disableNestedCalls = true;
this.SelectedValue = selectedValue;
this.SelectedIndex = selectedIndex;
_disableNestedCalls = false;
}
void InternalSelectedValueChanged()
{
if (_disableNestedCalls)
{
return;
}
if (String.IsNullOrWhiteSpace(this.SelectedValuePath))
{
return;
}
var selectedIndex = -1;
Object selectedItem = null;
var hasSelectedValuePath = !String.IsNullOrWhiteSpace(this.SelectedValuePath);
if (this.ItemsSource != null && hasSelectedValuePath)
{
var index = 0;
foreach (var item in this.ItemsSource)
{
if (item != null)
{
var type = item.GetType();
var prop = type.GetRuntimeProperty(this.SelectedValuePath);
if (prop.GetValue(item) == this.SelectedValue)
{
selectedIndex = index;
selectedItem = item;
Grasielly Silva
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)