0

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>
Matthias Herrmann
  • 2,650
  • 5
  • 32
  • 66

2 Answers2

0

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

P3N9U1N
  • 28
  • 2
  • So I did the following changes, but it isnt working: ObservableCollection offlinelist = new ObservableCollection(); and ItemsSource="{Binding offlinelist, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" and var newnote = new BoardNote(UserInput.Text); offlinelist.Add(newnote); doesnt update the ui – Matthias Herrmann Aug 21 '15 at 11:20
  • Do you have a property for your offlineList? It wont work with a field. Beside, you need to set the DataContext to your ViewModel, if you dont specify any additional options to your binding. – P3N9U1N Aug 21 '15 at 11:24
  • Thats not the issue, Datacontext and stuff like that is working fine, if i set the itemsource to the offlinelist after adding there items the items get displayed, but it doesnt update after it got binded. – Matthias Herrmann Aug 21 '15 at 11:33
  • Take a look at that: Here is the code of my listbox: http://stackoverflow.com/questions/32118282/windows-universal-listbox-items-acess-specific-ui-element/32134985#32134985 -> 1st answer – Matthias Herrmann Aug 21 '15 at 11:35
  • Well if you set the itemssource manually, you will overwrite the binding. Either expose your itemList as a property or set the binding to the itemssource in code. – P3N9U1N Aug 21 '15 at 11:41
  • i only set the itemsource now in xaml ... it was just to test it – Matthias Herrmann Aug 21 '15 at 11:50
0

You need to implement an ObservableCollection in your ViewModel in order to get updates.