How to add View in ListView?

I am working in one project, In which we are currently add another view by StackLayout by using StackLayout.Children.add(FilePath) is it possible to implement same custom cell with listview.

Best Answer

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @Neeraj_Kumar said:
    I am working in one project, In which we are currently add another view by StackLayout by using StackLayout.Children.add(FilePath) is it possible to implement same custom cell with listview.

    Specify the ItemTemplate property of ListView to implement a custom cell.
    To add an item to the ListView, assuming the ItemsSource property references an ObservableCollection, simply add an item to the ObservableCollection.

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited September 12

    Create a model class inheriting from INotifyPropertyChanged and set the collection of model_list to listview's itemSource. Change the content of list to notify the ListView.

    public partial class Page2 : ContentPage
    {
        public static ObservableCollection<_Model> list;
        public Page2()
        {
            InitializeComponent();
    
            list = new ObservableCollection<_Model>();
            list.Add(new _Model { content = "content_1" });
    
            listview.ItemsSource = list;
        }
    
        private void Button_Clicked(object sender, EventArgs e)
        {
            list.Add(new _Model { content = "content" });
        }
    }
    
    public class _Model : INotifyPropertyChanged
    {
        public string content;
        public string Content
        {
            get
            {
                return content;
            }
            set
            {
                if (content != value)
                {
                    content = value;
                    NotifyPropertyChanged();
                }
            }
        }
        protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
    
  • Neeraj_KumarNeeraj_Kumar Member ✭✭
    edited September 12

    I created different xaml file for designing of cardView now i want to add that file inside listview row programmatically. Like we add in stackLayout. Is this possible to add different xaml file to add inside listview?

  • Neeraj_KumarNeeraj_Kumar Member ✭✭

    I am new in xamarin.forms. I don't know how to implement this with TableView because in official documentation they not provide tutorial about this. Can you Please Help me? for learning how to implement dynamic table and how to add different xaml file in tablerow.

  • JohnHardmanJohnHardman GBUniversity mod

    @Jarvan said:

    I created different xaml file for designing of cardView now i want to add that file inside listview row programmatically.

    You can't do that on ListView. Try to use TableView instead.

    A ListView is best suited for a collections consisting of a single data type. This requirement is because only one type of cell can be used for each row in the list. The TableView control can support multiple cell types, so it is a better option when you need to display multiple data types.

    @Neeraj_Kumar -

    This is what ListView's DataTemplateSelector is for. See https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/selector

Sign In or Register to comment.