Reputation: 191
I am implementing a simple MVVM WPF application with simple databrowsing commands.
There is a xaml-window with a listbox
<ListBox x:Name="listBoxPersons" ItemsSource="{Binding Path=Persons}" SelectedIndex="{Binding Path=SelectedPerson, Mode=OneWayToSource}" Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="2" Margin="0,10,10,5" IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Besides there is a textbox to insert the where-clause
<TextBox Grid.Column="2" Grid.Row="6" Margin="0,5,10,5" Name="textBoxWhereClause" />
Persons is a ObservableCollection. I implemented simple Add / Delete / Change Commands for the persons. This works.
Question: what do i have to do, if the user performs a new query resulting in a completely new collection? How can a bind the new collection dynamically to the listbox?
I am a WPF beginner and helpless.
Any help is very appreciated!
Upvotes: 1
Views: 111
Reputation: 30097
How can I bind the new collection dynamically to the listbox?
You don't need to reset the binding. Binding is already there you only need to update the binding source.
Simply you have to replace the Persons
collection with newly retrieved results
You must have something similar to this in your View Model
private ObservableCollection<Person> _Person ;
public ObservableCollection<Person> Person
{
get
{
return _Person;
}
set
{
_Person = value;
OnPrpertyChanged("Person");
}
}
you can do Person = YourNewCollection;
//newly retrieved results
but there are about 50,000 persons in the table. This could result in performance difficulties
There are two things you can do
1) only retrieve limited number of records at a time and provide user next back buttons. Kind of paging.
2) use virtualization
option in ListBox so that UI can be responsive and efficient. It makes sure only those objects are loaded from Person collection into listbox which are can be displayed at a certain point of time
Upvotes: 1