Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Iterating through a ViewCell from the ItemSelected on a ListView

$
0
0

Hi,

I have the following code (it grabs the address book from the device)

public class AddressView : ContentPage
    {
        public AddressView()
        {
            this.Title = "Native address book";
            CreateView();
        }

        async void CreateView()
        {
            IAddress addresses = DependencyService.Get<IAddress>();
            var addressList = addresses.ContactDetails();
            if (addressList.Count == 0)
            {
                await DisplayAlert("No contacts", "Your phone has no contacts stored on it", "OK");
                return;
            }

            if (Device.OS != TargetPlatform.iOS)
                BackgroundColor = Color.White;
            else
                Padding = new Thickness(0, 20, 0, 0);

            var myList = new ListView()
            {
                ItemsSource = addressList,
                ItemTemplate = new DataTemplate(typeof(MyLayout))
            };
            myList.ItemSelected += MyList_ItemSelected;

            Content = myList;
        }

        void MyList_ItemSelected (object sender, SelectedItemChangedEventArgs e)
        {
            var item = e.SelectedItem as ViewCell;
        }
    }

    public class MyLayout : ViewCell
    {
        public MyLayout()
        {
            var label = new Label()
            {
                Text = "name",
                Font = Font.SystemFontOfSize(NamedSize.Default),
                TextColor = Color.Blue
            };

            var numberLabel = new Label()
            {
                Text = "number",
                Font = Font.SystemFontOfSize(NamedSize.Small),
                TextColor = Color.Black
            };

            this.BindingContextChanged += (object sender, EventArgs e) =>
            {
                var item = (KeyValuePair<string,string>)BindingContext;
                label.SetBinding(Label.TextProperty, new Binding("Key"));
                numberLabel.SetBinding(Label.TextProperty, new Binding("Value"));
            };

            View = new StackLayout()
            {
                Orientation = StackOrientation.Vertical,
                VerticalOptions = LayoutOptions.StartAndExpand,
                Padding = new Thickness(12, 8),
                Children = { label, numberLabel }
            };
        }
    }

The code works fine and the name and numbers show.

What I want to do is grab and store the phone number and name in the ViewCell returned by the ItemSelected event. Is there a way to do this grab (the store is easy enough)?

Thanks

Paul


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>