Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to refresh XAML view in MAUI?

I’m making an app where I have layouts generate dynamically based on a list.

<StackLayout>
        <CollectionView x:Name="taskList">
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="models:Task">
                    <VerticalStackLayout Margin="15">
                        <Entry Text="{Binding name}" IsReadOnly="True" />
                        <Entry Text="{Binding departmentsString}" IsReadOnly="True"/>
                        <HorizontalStackLayout>
                            <Entry Text="{Binding status}" IsReadOnly="True"/>
                            <Entry Text="{Binding deadline}" IsReadOnly="True" />
                            <Entry Text="{Binding author.fullName}" IsReadOnly="True"/>
                        </HorizontalStackLayout>
                        <Entry Text="{Binding description}" IsReadOnly="True" />
                    </VerticalStackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>

The list is bonded like this:

taskList.ItemsSource = company.tasks;

I want to refresh this whenever I add new items to the list.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried rebinding the list to the ItemsSource but it just didn’t work:

taskList.ItemsSource = company.tasks;

How should I do it? Can I just refresh the view so it generates everything again?

>Solution :

You should use an IEnumerable collection that sends property change notifications(such as ObservableCollection) for the ItemsSource, then simply add to the underlying collection.
Here’s an example:

ObservableCollection<string> tasks = new();
taskList.ItemsSource = tasks;
tasks.Add("Do something");

Microsoft Reference

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading