Search code examples
c#xamlbindinglistboxinotifycollectionchanged

Listbox UI Update after adding Item to a list of objects


I got this list:

  List<BoardNote> offlinelist = new List<BoardNote>();

It is binded to my Listbox "boardlist". Now I want that the UI of the Listbox gets updated everytime after adding a new Boardnote. I know already that i should use "INotifyCollectionChanged" but I am overstrained to do this. Here is the code of the "boardlist":

 <ListBox x:Name="BoardList" ItemsSource="{Binding offlinelist}" > //I need to add binding mode two way and property changed idk
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                            <TextBox IsReadOnly="True" ScrollViewer.VerticalScrollBarVisibility="Visible" Text="{Binding text}" TextWrapping="Wrap" Foreground="DarkBlue"></TextBox>
                            <AppBarButton Visibility="{Binding visibility}" Icon="Globe" Click="OpenInBrowser" x:Name="Link"></AppBarButton>
                            <AppBarButton Icon="Copy" Click="Copy"></AppBarButton>
                            <AppBarButton Icon="Delete" Click="Delete"></AppBarButton>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

Solution

  • You can use ObservalbleList instead of List. It already implements INotifyCollectionChanged for you.