Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Bindable Picker

$
0
0

Hi everyone, I'm trying to create a contentview where there is a picker inside and a button to clean the picker.

I've managed to bind the property (ItemDisplayBinding) and the others do not work well, how can I do it?

            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MySednaApp.CustomControl.sPickerCust"
             x:Name="sPickerContentView">
    <ContentView.Content>
        <Grid x:Name="grid">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Image Grid.Column="0" x:Name="searchButton" Source = "ico_search_entry.png" WidthRequest="18" HeightRequest="18">
            </Image>
            <Picker Grid.Column="1" x:Name="picker"
                     ItemsSource="{Binding Source={x:Reference sPickerContentView}, Path=Items_Source}"
                     SelectedItem="{Binding Source={x:Reference sPickerContentView}, Path=Selected_Item}"/>
            <Image Grid.Column="2" x:Name="clearbutton" Source = "ico_clear_entry.png" WidthRequest="18" HeightRequest="18" IsVisible="{Binding ShowClearButton}">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="TapGestureRecognizer_OnTapped"/>
                </Image.GestureRecognizers>
            </Image>

        </Grid>
    </ContentView.Content>
</ContentView>

my code bheind:

namespace MySednaApp.CustomControl
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class sPickerCust
    {
        public sPickerCust()
        {
            InitializeComponent();
            ShowClearButton = false;
        }

        public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
                                                                        nameof(Items_Source),
                                                                        typeof(IEnumerable),
                                                                        typeof(Picker));

        public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create(
                                                                        nameof(Selected_Item),
                                                                        typeof(object),
                                                                        typeof(Picker));


        //public static readonly BindableProperty ItemDisplayBindingProperty = BindableProperty.Create(
        //                                                                       nameof(ItemDisplay_Binding),
        //                                                                       typeof(BindingBase),
        //                                                                       typeof(Picker),new Binding());
        public IEnumerable Items_Source
        {
            get { return (IEnumerable)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        public object Selected_Item
        {
            get { return (object)GetValue(SelectedItemProperty); }
            set
            {
                SetValue(SelectedItemProperty, value);
            }
        }
        //public BindingBase ItemDisplay_Binding
        //{
        //    get
        //    {
        //        try
        //        {
        //           // var i = (BindingBase)GetValue(ItemDisplayBindingProperty);
        //            var i = new Binding("Descrizione");
        //            return i;
        //        }
        //        catch (Exception e)
        //        {
        //            Debug.WriteLine(e);
        //            return null;
        //        }

        //    }
        //    set
        //    {
        //        SetValue(ItemDisplayBindingProperty, value);
        //    }
        //}
        private string displayMember;
        public string DisplayMember
        {
            get { return displayMember; }
            set
            {
                try
                {
                    displayMember = value;
                    picker.ItemDisplayBinding = new Binding(displayMember);

                }
                catch (ArgumentException ex)
                {
                    Debug.WriteLine(ex);
                }
            }
        }

        private bool showClearButton;
        public bool ShowClearButton
        {
            get { return showClearButton; }
            set
            {
                try
                {
                    showClearButton = value;
                    clearbutton.IsVisible = showClearButton;

                }
                catch (ArgumentException ex)
                {
                    Debug.WriteLine(ex);
                }
            }
        }

        private void TapGestureRecognizer_OnTapped(object sender, EventArgs e)
        {
            picker.SelectedIndex = -1;
        }
    }
}

and in my view:

                         <customControl:sPickerCust
                                            ShowClearButton="True"
                                            DisplayMember="Descrizione"
                                            ItemsSource="{Binding ListaTipoArticolo}"
                                            SelectedItem="{Binding SelectedItem}" />

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>