I have a MAUI app. I have an object Comment
with a List<Note>
of Note
-objects inside:
public class Comment {
public List<Note> Notes { get; set; }
...
}
Now in my MainPage.xaml
I want to display all Note
s of my Comment
. For this I have built a <ListView>
:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="VisitMaui.MainPage"
xmlns:viewmodel="clr-namespace:VisitMaui.ViewModels"
x:DataType="viewmodel:MainViewModel"
xmlns:Classes="clr-namespace:PersonalBibleindex.Classes"
xmlns:Controls="clr-namespace:VisitMaui.Controls">
<Label Text="Notes/>
<ListView ItemsSource="{Binding Comment.Notes}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="{x:Type Classes:Note}">
<TextCell Text="{Binding Text}"></TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
If I load Note
s into my List manually this works fine. But now I want to create Note
s dynamically by clicking on a button:
<Button Text="Add note" Command="{Binding AddCommand}"/>
Whereas the Add
-function in my ViewModel looks like this:
[RelayCommand]
void Add()
{
Note MyNote = new Note();
MyNote.VerseWords = "DasWort";
MyNote.Text = "Testnotiz";
Comment.Notes.Add(MyNote);
Counter++;
}
My problem: When I click on the Add note
-Button, a new note will be added to Comment.Notes
-List, but the UI doesn't refresh. What am I missing?