Forum Xamarin.iOS

ViewCell PropertyChanged event not working on iOS

Bert-JanBert-Jan USMember ✭✭
edited August 2020 in Xamarin.iOS

I use a listview to display items in my Xamarin Forms app (Android and iOS). When an item is selected, I use the PropertyChanged event of the ViewCell to change the selected item color and to add detail information to the selected item.

On Android this works fine. In xaml I specify the method:

<ViewCell PropertyChanged="ViewCell_PropertyChanged">

And in the code-behind file there is a method defined:

        private void ViewCell_PropertyChanged(object sender, PropertyChangedEventArgs e)
            if (e.PropertyName == "IsSelected")
            {.... etc.

Why does this not work on iOS?
Hope anybody can help..

Best Answer


  • LandLuLandLu Member, Xamarin Team Xamurai

    There are different effects on different platforms.
    ViewCell renders to UITableViewCell on iOS. It has a content view to show up the highlighted effect. So it doesn't have a selected state on itself.
    I suggest adding a tap gesture on the root layout of the view cell like:

                <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>

    Therefore, you could know which cell is selected and change its background in the event.

  • Bert-JanBert-Jan USMember ✭✭
    edited August 2020

    Thank you. The problem is that if I add a TapGestureRecognizer in this way, the ListView's SelectedItem is no longer set when an Item is tapped. I noticed that ListView.ItemTapped is no longer triggered.
    <ListView ItemTapped="Projects_ItemTapped" <- this method is no longer called. Any idea?

  • Bert-JanBert-Jan USMember ✭✭

    Thank you. Works fine now. Remark: I do have to call cell.ForceUpdateSize() to get the cell to resize on iOS (otherwise only the background colour is updated, but grids with details are not shown (IsVisible bound))

            private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
                var layout = sender as StackLayout;
                if (layout.Parent != null)
                    ViewCell cell = (ViewCell)layout.Parent;
                var selectedItem = (ProjectListItem) layout.BindingContext;
Sign In or Register to comment.