Home > Blockchain >  How do I set ItemDisplayBinding for a Picker inside a Content View
How do I set ItemDisplayBinding for a Picker inside a Content View

Time:04-03

I have a ContentPage, which has a reference to a ContentView. Inside this content view I have a custom Picker control, I have binded other properties e.g. ItemsSource, ItemSelected but ItemDisplayBinding is being a pain. I can see in the code for the Xamarin Forms Picker class that it's not a straightforward bindable property field like the rest. At the moment I am getting this error when I click on the Picker. I know it that specific display binding property because the picker works fine without me initialising it.

Error

Java.Lang.NullPointerException
  Message=Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

Content Page

<controls:MyPickerView PickerTitle="Select time period"
                                       PickerItemDisplayBinding="{Binding Display}" 
                                       PickerItemsSource="{Binding AppointmentRangeOptions}"/>

MyPickerView.xaml

<ContentView.Content>
        <Frame >
            <Picker Title = "{Binding PickerTitle}"
                                   HorizontalOptions="FillAndExpand"
                                   IsEnabled="{Binding PickerIsEnabled}"
                                   ItemDisplayBinding="{Binding PickerItemDisplayBinding}" 
                                   SelectedItem="{Binding SelectedItem, Mode=TwoWay}" 
                                   ItemsSource="{Binding PickerItemsSource, Mode=OneWay}"/>
        </Frame>
    </ContentView.Content>

MyPickerView.xaml.cs

public partial class MyPickerView : ContentView
{
    public MyPickerView()
    {
        InitializeComponent();
        Content.BindingContext = this;
    }

    public static readonly BindableProperty PickerIsEnabledProperty = BindableProperty.Create(
        nameof(PickerIsEnabled),
        typeof(bool),
        typeof(MyPickerView),
        null);

    public bool PickerIsEnabled
    {
        get => (bool)GetValue(PickerIsEnabledProperty);
        set => SetValue(PickerIsEnabledProperty, value);
    }

    public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create(
        nameof(SelectedItem),
        typeof(object),
        typeof(MyPickerView),
        null);

    public object SelectedItem
    {
        get => GetValue(SelectedItemProperty);
        set => SetValue(SelectedItemProperty, value);
    }

    public static readonly BindableProperty PickerTitleProperty = BindableProperty.Create(
        nameof(PickerTitle),
        typeof(string),
        typeof(MyPickerView),
        null);

    public string PickerTitle
    {
        get => (string)GetValue(PickerTitleProperty);
        set => SetValue(PickerTitleProperty, value);
    }

    public static readonly BindableProperty PickerItemDisplayBindingProperty = BindableProperty.Create(
        nameof(PickerItemDisplayBinding),
        typeof(string),
        typeof(MyPicker)
        );

    public string PickerItemDisplayBinding
    {
        get => (string)GetValue(PickerItemDisplayBindingProperty);
        set => SetValue(PickerItemDisplayBindingProperty, value);
    }

    public static readonly BindableProperty PickerItemsSourceProperty = BindableProperty.Create(
        nameof(PickerItemsSource),
        typeof(IList),
        typeof(MyPickerView));

    public IList PickerItemsSource
    {
        get => (IList)GetValue(PickerItemsSourceProperty);
        set => SetValue(PickerItemsSourceProperty, value);
    }
}

CodePudding user response:

Indeed, this property is a pain. I solved it with a little trick:

MyPickerView.xaml.cs

public BindingBase ItemDisplayBinding
{
    get => MyPicker?.ItemDisplayBinding;
    set => MyPicker.ItemDisplayBinding = value;
} // no need to define BindableProperty

MyPickerView.xaml

<ContentView.Content>
    <Frame>
        <Picker x:Name="MyPicker" />
    </Frame>
</ContentView.Content>

Content Page

<controls:MyPickerView 
    PickerTitle="Select time period"
    ItemDisplayBinding="{Binding Display}" 
    PickerItemsSource="{Binding AppointmentRangeOptions}"/>
  • Related