Forum Xamarin.iOS
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

ViewCell PropertyChanged event not working on iOS

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

Hi,
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..
Regards,
Bert-Jan

Best Answer

Answers

  • 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:

    <ViewCell>
        <StackLayout>
            <StackLayout.GestureRecognizers>
                <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
            </StackLayout.GestureRecognizers>
    
            <!--...-->
        </StackLayout>
    </ViewCell>
    

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

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

    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-Jan

  • 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;
                    cell.ForceUpdateSize();
                }
                var selectedItem = (ProjectListItem) layout.BindingContext;
                ...
    
Sign In or Register to comment.