-1

im adding items to my listbox but anytime I want to add them I need to remove and add the new itemsource, is there any way to make the listbox update automatically so I dont need to remove and add the ItemsSource? Thanks!

Im currently using an ObservableCollection.

Sickness
  • 33
  • 1
  • 8
  • 2
    Your question doens't make sense. If you're using an `ObservableCollection` then you assign (or bind) it once to ItemsSource and that's it. Updates are carried out by adding or removing items from the ObservableCollection instance. – Peregrine Mar 05 '21 at 11:16
  • @Peregrine the thing is that when I add items to my list there isnt any update, thats why I made this question. – Sickness Mar 05 '21 at 11:34
  • Please show us your code then - it's impossible to comment otherwise. – Peregrine Mar 05 '21 at 11:36

2 Answers2

1

I think you missunderstood the concept of Observable Collection, because it provides exactly the functionality you need.

The Observable Collection already has built in functionalities like refreshing the UI, that means when you update your collection from code behind or ViewModel, the UI will update too.
Useful link: What is the use of ObservableCollection in .net?

Stan1k
  • 338
  • 2
  • 17
-2

You can use Data Binding to bind to that collection and then have the listbox populate automatically from whatever is in that. This will be a listbox of ticked checkboxes with "wow" as the text for however many are in the collection.

<ListBox Name="testList" ItemsSource="{Binding CollectionName}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox IsChecked="true">
                    <TextBlock Text="{Binding Content}"/>
                </CheckBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>


public class ItemInCollection
{
      public string Content = "wow";
}
TMStackO
  • 313
  • 3
  • 13