Forum Xamarin Xamarin.Forms

How can I access the ViewCell that was selected in a ListView ItemSelected event?

Zzz96Zzz96 Member ✭✭

Hi all,

I'm trying to access the ViewCell that was selected in a ListView.ItemSelected event. I know how the access the ListView itself and the selected object, but I'm trying to access the actual ViewCell that was selected to change its height (either using the Cell.ForceUpdateSize() method or changing the Cell.Height property). I managed to find a sample on the Xamarin website (Dynamic ListView Row Resizing) in which the ViewCell was accessed as the parent (parent of the parent, to be exact) of a clicked Image that was in the ViewCell, but I'm wondering if there's a way of doing that from the ListView.ItemSelected event. I can't seem to find anything in the documentation that fits the bill.

Thank you!

Answers

  • MabroukMabrouk USMember ✭✭✭

    Hi,
    i really dont know if i understand you well, but did you try to set the property HasUnevenRows="True" of your ListView? That each row can have different heigth.

    Or maybe you can user the DataTemplateSelector. Take a look here.

    I hope that helps,
    Mabrouk.

  • Zzz96Zzz96 Member ✭✭

    Hi @Mabrouk,

    Thank you for your reply! Unfortunately, that's not what I'm looking for...

    First, HasUnevenRows was already set to true and that makes the app work beautifully on Android, but in iOS, the ViewCell is not automatically resized and has to be manually set.

    More importantly, I just want to know how to refer to the ViewCell that was clicked in a ListView.ItemSelected event; modifying Cell.Height was just an example of why anyone would want to do that. The sender object of the event refers to the whole ListView and SelectedItemChangedEventArgs can only be used to refer to the bound object, so I don't see how I can point to the specific ViewCell that was selected.

    Another way to think of the problem is this: how can you programmatically refer to a particular ViewCell in a ListView? For example, is it possible to have a button that sets the Cell.IsEnabled property of, say, the third ViewCell to false?

  • AlmaJensen.9398AlmaJensen.9398 USMember ✭✭✭

    I've never tried this with a Viewcell but the only way I can think of to access it programatically would be to bind to properties of the viewcell. Look at the properties available on the viewcell and see if the viewcell itself has any bindable properties or events. You can extend the viewcell then to trigger other functions available to that viewcell.

  • Zzz96Zzz96 Member ✭✭

    @AlmaJensen.9398 Thank you for your suggestion! I actually did try that as well but I get this error message:

    Xamarin.Forms.Xaml.XamlParseException: Position 13:27. Cannot assign property "Height": Property does not exists, or is not assignable, or mismatching type between value and property

  • Zzz96Zzz96 Member ✭✭

    @AlmaJensen.9398 Just for your reference, I've included a simple sample that generates this error:

    <?xml version="1.0" encoding="utf-8" ?>
    <!--Namespaces removed because I can't post links-->
    <ContentPage xmlns:local="clr-namespace:CellHeightBindingTest"
                 x:Class="CellHeightBindingTest.MainPage">
        <ContentPage.BindingContext>
            <local:Data />
        </ContentPage.BindingContext>
    
        <ListView ItemsSource="{Binding SomeList}" HasUnevenRows="True" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell Height="{Binding CellHeight}">
                        <Label Text="{Binding StrProperty}" />
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    
    </ContentPage>
    

    The code-behind is just InitializeComponent(); and Data.cs is a simple ViewModel:

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Text;
    
    namespace CellHeightBindingTest
    {
        public class SomeClass
        {
            public string StrProperty { get; set; }
            public double CellHeight { get; set; }
        }
    
        class Data
        {
            public ObservableCollection<SomeClass> SomeList { get; set; }
    
            public Data()
            {
                SomeList = new ObservableCollection<SomeClass>
                {
                    new SomeClass {
                        StrProperty ="Something 1",
                        CellHeight =40
                    },
                    new SomeClass {
                        StrProperty ="Something 2",
                        CellHeight =50
                    },
                    new SomeClass {
                        StrProperty ="Something 3",
                        CellHeight =60
                    }
                };
            }
        }
    
    }
    
  • mirkarammirkaram Member ✭✭

    @Zzz96 You can achieve this functionality by just adding TapGestureRecognizer to your ViewCell child whatever element it is, but it must has GestureRecognizers property. You can also use other controls like button that has an event for accesing that element, example ;

                <Label>
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
                </Label.GestureRecognizers>
              </Label>
    

    And now get extract all elements of ViewCell, their parent, child, siblings and blinded object of viewcell, like in below implemented tapped event of label.

        private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
            var tapped_label = sender as Label;
            var your_viewcell = tapped_label.Parent as ViewCell;
            var your_binded_object = your_viewcell.BindingContext;
           }
    
  • RamazanRamazan Member ✭✭

    @Zzz96 Try to use TapGestureRecognizer at the same the time with selectedItem. Kindle reminder i achieve that on UWP but android could give error.

Sign In or Register to comment.