Mark CHoi Mark CHoi - 6 months ago 33
C# Question

binding listbox and refresh data in wpf

I've bound one listbox between property variable and ItemSource of Listbox in XAML with MVVM pattern.


<ListBox Name="lbTemp" ItemsSource="{Binding SrcPermissionList}"/>


public List<CheckBox> SrcPermissionList= new List<CheckBox>();
CheckBox ckbox = new CheckBox();

Then it caused a STA thread accessing issue because I tried to add the checkbox in dynamic way. As for the data it's obtaining by Service+Server. So I used Dispatch code to fix that.

Application.Current.Dispatcher.Invoke(new Action(delegate ...

After all, it kinda seems like doesn't work but when I debugged, it actually has data inside. Before try to manipulate the listbox such as clicking it or scrolling it down, it renders nothing as shown below.

Image of listbox which is not working

I donno how to refresh the bound listbox without touching the listbox.
Data show at least I click only 1 time, or only 1 tick of scrolling.
I think it may have problem among the threads. and UI thread doesn't know when the itemsource came.
What am I doing wrong?


You should use ObservableCollection

ObservableCollection is a collection that allows code outside the collection be aware of when changes to the collection (add, move, remove) occur.

 private ObservableCollection<Type> collection;