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

BUG: Can't add multiple items to ListView (ArgumentOutOfRangeException)

$
0
0

I have confirmed this with both v1.1.1 and v1.2.1

Repro

  1. create new PCL project
  2. update Xamarin.Forms
  3. copy the below code into your App.cs
  4. run

    public class App
    {
        public static Page GetMainPage()
        {
            return new NavigationPage(new RootPage());
        }
    }
    
     public class RootPage : ContentPage
     {
        private ListView _listView;
    
        public RootPage()
        {
            var vm = new RootPageViewModel();
            Content = BuildListView(vm);
        }
    
        private StackLayout BuildListView(RootPageViewModel viewModel)
        {
            var headerTemplate = new DataTemplate(typeof(ModuleMediaListHeaderTemplate));
            headerTemplate.CreateContent();
    
            var itemTemplate = new DataTemplate(typeof(ModuleMediaListItemTemplate));
            itemTemplate.CreateContent();
    
            _listView = new ListView
            {
                ItemsSource = viewModel.MediaSections,
                IsGroupingEnabled = true,
                GroupDisplayBinding = new Binding("SectionName"),
                HasUnevenRows = false,
                GroupHeaderTemplate = headerTemplate,
                ItemTemplate = itemTemplate
            };
    
            return new StackLayout { Children = { _listView } };
        }
    }
    
    public class RootPageViewModel
    {
        public IEnumerable MediaSections
        {
            get
            {
                var titles = new[] {"First", "Second", "Third", "Forth", "Fifth"};
    
                return titles.Select(section => new MediaListSection(section)
                {
                    new FooViewModel {Title = "Foo", Description = "description for foo"}, 
                    new FooViewModel {Title = "Bar", Description = "description for bar"}, 
                    new FooViewModel {Title = "Baz", Description = "description for baz"}, 
                    new FooViewModel {Title = "Fiz", Description = "description for fiz"}, 
                    new FooViewModel {Title = "Buz", Description = "description for buz"},
                }).ToList();
            }
        }
    }
    
    public class MediaListSection : ObservableCollection<FooViewModel>
    {
        public string SectionName { get; private set; }
    
        public MediaListSection(string sectionName)
        {
            SectionName = sectionName;
        }
    }
    
    public class FooViewModel
    {
        public string Title { get; set; }
        public string Description { get; set; }
    }
    
    public class ModuleMediaListItemTemplate : ViewCell
    {
        public ModuleMediaListItemTemplate()
        {
            var title = new Label { YAlign = TextAlignment.Center };
            title.SetBinding(Label.TextProperty, new Binding("Title", BindingMode.OneWay));
    
            var description = new Label { YAlign = TextAlignment.Center };
            description.SetBinding(Label.TextProperty, new Binding("Description", BindingMode.OneWay));
    
            View = new StackLayout
            {
                Orientation = StackOrientation.Horizontal,
                Padding = new Thickness(8),
                Children = { title, description } 
            };
        }
    }
    
    public class ModuleMediaListHeaderTemplate : ViewCell
    {
        public ModuleMediaListHeaderTemplate()
        {
            var title = new Label { TextColor = Color.White, YAlign = TextAlignment.Center };
            title.SetBinding(Label.TextProperty, new Binding("SectionName", BindingMode.OneWay));
    
            View = new StackLayout
            {
                Padding = new Thickness(8, 0),
                BackgroundColor = Color.FromHex("#999999"),
                Orientation = StackOrientation.Horizontal,
                Children = { title },
            };
        }
    }
    

Viewing all articles
Browse latest Browse all 58056

Trending Articles



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