CustomControl: SelectedItem won't be fired

DominikNehlDominikNehl ATMember
edited November 8 in Xamarin.Forms

I created a customcontrol with the following bindable properties:
CustomControl.cs:

public static readonly BindableProperty ItemSourceProperty = 
            BindableProperty.Create("ItemSource", typeof(IEnumerable), typeof(CustomScrollView), default(IEnumerable));

public static readonly BindableProperty SelectedItemProperty = 
            BindableProperty.Create("SelectedItem", typeof(object), typeof(CustomScrollView), null, BindingMode.OneWayToSource,
            propertyChanged: OnSelectedItemChanged);

Xaml:

<controls:CustomScrollView
                            Orientation="Horizontal"
                            ItemSource="{Binding Images}"
                            SelectedItem="{Binding SelectedImage, Mode=TwoWay}"
                ItemIsSelected="{Binding ItemIsSelected, Mode=TwoWay}"
                            HeightRequest="200"
                            >
                            <controls:CustomScrollView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <StackLayout Padding="5">
                                            <Image
                                                Source="{Binding Source}"
                                                HeightRequest="80"
                                                WidthRequest="80"/>
                                            <Button
                                                Text="Delete"
                        Visible="Binding ItemIsSelected"
                                                Command="{Binding DeleteSelectedImageCommand}"
                                        </StackLayout>
                                    </ViewCell>
                                </DataTemplate>
                            </controls:CustomScrollView.ItemTemplate>
                        </controls:CustomScrollView>

viewModel

public Image SelectedImage
        {
            get => _selectedImage;
            set => SetProperty(ref _selectedImage, value); //incl. onpropertychangedevent
        }

@Edit:

private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var customScrollView = (CustomScrollView) bindable;
            customScrollView.ItemSelected(customScrollView, new SelectedItemChangedEventArgs(newValue));
        }

When I click on an item, the SelectedItem is still null..

Sign In or Register to comment.