I do not know how many columns and column name in a listview?

mc 4,616 Reputation points
2023-05-01T01:58:54.9+00:00

I use a ListView But I do not know how many columns is in it it is dynamic.

the only know column is Id

I can set the binding of Id

how to set others binding?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,596 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,551 Reputation points Microsoft Vendor
    2023-05-02T01:51:42.73+00:00

    Hello,

    ==========Update=========

    how to bind it to Label?

    You can bind it with  <Label Text="{Binding .}"></Label> diretly like following layout.

     <ListView x:Name="mysl">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="{Binding .}"></Label>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    

    Here is background code.

    public NewPage1()
        {
            InitializeComponent();
    
           ObservableCollection<string> Dates = new ObservableCollection<string>();
    
    
           Dates.CollectionChanged += Dates_CollectionChanged; ;
            for (int i = 0; i < 10; i++)
            {
                Dates.Add(i.ToString());
            }
            mysl.ItemsSource = Dates;
        }
    
     private void Dates_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            var values=sender as ObservableCollection<string>;
           
            var valuecount=values.Count;
            var name = e.NewItems;
       }
    

    You can use ObservableCollection<string> instead of string[].

    And add CollectionChanged delegate, when you add or remove item in the ObservableCollection, Dates_CollectionChanged method will be execute. You can get count by values.Count;, get the added column name by e.NewItems;

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.