This exception can occur when attempting to bind a collection to a non-bindable property when XAML pre-compilation is enabled. A common example is attempting to bind to Picker.Items
. See below.
This exception can occur when attempting to bind a collection to a non-bindable property when XAML pre-compilation is disabled. A common example is attempting to bind to Picker.Items
. See below.
Picker.Items
Property Is Not BindableThis code will cause an error:
<!-- BAD CODE: will cause an error -->
<Picker Items="{Binding MyViewModelItems}" SelectedIndex="0" />
The exception may be one of the following:
System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.
or
System.ArgumentException: Object of type 'Xamarin.Forms.Binding' cannot be converted to type 'System.String'.
Specifically, the Items
property is non-bindable. Solutions include creating your own custom control or using a third-party control, such as BindablePicker
from FreshEssentials. After installing the FreshEssentials NuGet package in your project, the package's BindablePicker
control with a bindable ItemsSource
property is available:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:fe="clr-namespace:FreshEssentials;assembly=FreshEssentials"
xmlns:my="clr-namespace:MyAssembly;assembly=MyAssembly"
x:Class="MyNamespace.MyPage">
<ContentPage.BindingContext>
<my:MyViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
<fe:BindablePicker ItemsSource="{Binding MyViewModelItems}" SelectedIndex="0" />
</ContentPage.Content>
</ContentPage>