I have a ContentView with a bindableproperty of type IEnumerable. When I bind the property to the ItemsSource of a CollectionView it works. Then I added the CollectionView to the SelectorTemplate of a TemplatedPicker with the same binding syntax it doesn't work. What is the correct binding syntax for the ItemsSource of a CollectionView inside a TemplatedPicker?
Here the code:
ColorPicker.xaml (with CollectionView only)
<CollectionView
ItemsSource="{Binding Source={x:Reference this}, Path=ViewColorList}"
HeightRequest="320"
SelectionMode="Single">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:ColorData">
<Grid HeightRequest="70" WidthRequest="100" RowDefinitions="Auto, Auto">
<telerik:RadBorder Grid.Row="0" WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
HorizontalOptions="Center"
VerticalOptions="Center"
BorderColor="{Binding ColorValue, Converter = {StaticResource ColorConverter}}"
BorderThickness="2"
BackgroundColor="{Binding ColorValue, Converter = {StaticResource ColorConverter}}" />
<Label Grid.Row="1" Text="{Binding Name}" FontSize="12" TextColor="Black" HorizontalOptions="Center"/>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="4" />
</CollectionView.ItemsLayout>
</C ollectionView>
ColorPicker.xaml (with TemplatedPicker)
<telerik:RadTemplatedPicker x:Name="picker"
Placeholder="Select a Color">
<telerik:RadTemplatedPicker.DisplayTemplate>
<ControlTemplate>
<Grid ColumnDefinitions="Auto, *">
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{TemplateBinding ToggleCommand}" />
</Grid.GestureRecognizers>
<telerik:RadBorder
Grid.Column="0"
WidthRequest="20"
HeightRequest="20"
CornerRadius="10"
VerticalOptions="Center"
BorderColor="{Binding Source={x:Reference ColorPicker}, Path=ViewSelectedColor.ColorValue, Converter = {StaticResource TransparentConverter}}"
BorderThickness="1"
BackgroundColor="{Binding Source={x:Reference ColorPicker}, Path=ViewSelectedColor.ColorValue, Converter = {StaticResource ColorConverter}}"/>
<Label Grid.Column="1" Text="{Binding Source={x:Reference ColorPicker}, Path=ViewSelectedColor.Name}" FontSize="15" TextColor="Black" Margin="12, 0, 0, 0"/>
</Grid>
</ControlTemplate>
</telerik:RadTemplatedPicker.DisplayTemplate>
<telerik:RadTemplatedPicker.SelectorTemplate>
<ControlTemplate>
<CollectionView
ItemsSource="{Binding Source={x:Reference this}, Path=ViewColorList}"
HeightRequest="320"
SelectionMode="Single">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:ColorData">
<Grid HeightRequest="70" WidthRequest="100" RowDefinitions="Auto, Auto">
<Grid.GestureRecognizers>
<TapGestureRecognizer Tapped="Close_ColorSelection" />
</Grid.GestureRecognizers>
<telerik:RadBorder Grid.Row="0" WidthRequest="40"
HeightRequest="40"
CornerRadius="20"
HorizontalOptions="Center"
VerticalOptions="Center"
BorderColor="{Binding ColorValue, Converter = {StaticResource ColorConverter}}"
BorderThickness="2"
BackgroundColor="{Binding ColorValue, Converter = {StaticResource ColorConverter}}" />
<Label Grid.Row="1" Text="{Binding Name}" FontSize="12" TextColor="Black" HorizontalOptions="Center"/>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="4" />
</CollectionView.ItemsLayout>
</CollectionView>
</ControlTemplate>
</telerik:RadTemplatedPicker.SelectorTemplate>
</telerik:RadTemplatedPicker>
ColorPicker.xaml.cs
public partial class ColorPicker : ContentView
{
public ColorPicker()
{
InitializeComponent();
}
#region ViewColorList property
public static readonly BindableProperty ViewColorListProperty = BindableProperty.Create(
nameof(ViewColorList),
typeof(IEnumerable<ColorData>),
typeof(ColorPicker3),
default(IEnumerable<ColorData>),
propertyChanged: OnItemsSourcePropertyChanged);
public IEnumerable<ColorData> ViewColorList
{
get => (IEnumerable<ColorData>)GetValue(ViewColorListProperty);
set => SetValue(ViewColorListProperty, value);
}
#endregion
}
ColorData.cs (Model of ColorData)
public class ColorData
{
public string Name { get; set; }
public string ColorValue { get; set; }
}