Access a custom cell element directly via C# ??

wehweh USMember ✭✭

I have a custom cell for a ListView that is two Labels and an Image and I'd like to know how I can access
the Image item directly from C#?

After adding items to a ListView, I'm using "ItemTapped" to call a method in which I want to change the image contents.
Think of it like an iOS TableView cell where you can put it into "edit mode" then tap the cell to show a checkmark.
However, I cannot figure out how to access the Image.

Here's the "ItemTapped" method:

        void HandleTappedItem(object sender, ItemTappedEventArgs e)
        {
            if (e.Item == null)
            {
                return;
            }

            if (editMode)
            {
                //add/remove check image

                var tmpCell = (CellDraftList)sender;

                // I'd like to be able to do something like this:
               //  tmpCell.View.checkImage.Source = "circlecheck";

            }
            else
            {
                Debug.WriteLine("**** should push to new page - create issue filled with data");
            }
        }

Here's the Custom Cell:

    using System;
    using Xamarin.Forms;

    namespace IssueLogic
    {
        public class CellDraftList : ViewCell
        {
            public CellDraftList()
            {
                Color labelBackColor = Color.FromHex("ffffff");
                Color labelForeColor = Color.FromHex("000000");
                int labelFontSize = 12;

                //Main stack
                StackLayout mainStack = new StackLayout();
                mainStack.Orientation = StackOrientation.Horizontal;
                mainStack.HorizontalOptions = LayoutOptions.FillAndExpand;
                mainStack.VerticalOptions = LayoutOptions.FillAndExpand;

                // one value - same for l,t,r,b  two values - l/r, t/b or horiz and vert
                mainStack.Margin = new Thickness(0, 5, 0, 5); //around edges between mainStack and Cell
                mainStack.Padding = new Thickness(0, 0, 0, 0); ;
                mainStack.Spacing = 0;

                Label descPreLabel = new Label();
                descPreLabel.HorizontalOptions = LayoutOptions.Start;
                descPreLabel.VerticalOptions = LayoutOptions.Center;
                descPreLabel.FontSize = labelFontSize;
                descPreLabel.Text = " Description: ";
                descPreLabel.BackgroundColor = labelBackColor;
                Label descLabel = new Label();
                descLabel.SetBinding(Label.TextProperty, "Description");
                descLabel.HorizontalOptions = LayoutOptions.FillAndExpand;
                descLabel.VerticalOptions = LayoutOptions.FillAndExpand;
                descLabel.FontSize = labelFontSize;
                descLabel.TextColor = labelForeColor;
                descLabel.BackgroundColor = labelBackColor;
                descLabel.HeightRequest = 60;

                var checkImage = new Image();
                checkImage.Source = "nocheck";
                checkImage.Aspect = Aspect.AspectFit;
                checkImage.HorizontalOptions = LayoutOptions.End;
                checkImage.VerticalOptions = LayoutOptions.Center;
                checkImage.BackgroundColor = Color.FromHex("eeeeee");

                //ADD ITEMS TO STACKS
                mainStack.Children.Add(descPreLabel);
                mainStack.Children.Add(descLabel);
                mainStack.Children.Add(checkImage);
                View = mainStack;
            }
        }
    }

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    I think you can take a look to

    https://github.com/acaliaro/TestBindingWithListView

    Where there are some images that change when you click... hope this help
  • wehweh USMember ✭✭

    Alessandro - Thanks but it looks like everything in your sample is using Binding. Is there a way to do this without Binding the Image?

  • wehweh USMember ✭✭

    OK, got it working! It was much simpler than I thought - I added an "ImageName" string to my Model , then bound it to the Image in my custom cell - easy! Thanks for the direction...

Sign In or Register to comment.