Reputation: 630
So I have a DataGrid
in my UserControl
. It is databound to an observable collection
in My ViewModel. The ViewModel implements the INotifyPropertyChanged
interface and has been appropriately wired up in the view.
In my view:
<DataGrid AutoGenerateColumns="False"
EnableRowVirtualization="True"
ItemsSource="{Binding ContributionCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<!-- DataGrid rows -->
/>
And in The ViewModel:
private ObservableCollection<contribution> _contributionCollection;
public ObservableCollection<contribution> ContributionCollection
{
get
{
return _contributionCollection;
}
set
{
_contributionCollection= value;
OnPropertyChanged("ContributionCollection");
}
}
What is meant to happen, is that; At a click of a specific button, a query fires against the database and brings back some results. Those results are meant to be shown in the Datagrid.
// Fired by an ICommand Property
public object Contributions_CommandExecute(object param)
{
var queryContributions = context.contributions.Where(c => c.member == Member);
return _contributionCollection = new ObservableCollection<contribution>(queryContributions);
}
But the datagrid always comes up empty. The Application freezes for a bit, suggesting something is happening in the background, but nothing is actually conveyed on the view. In fact, the only time it works properly, is if the ViewModel constructor fires the query upon initialization. But then after that, It won't fire again.
Any idea where I'm going wrong?
Upvotes: 0
Views: 59
Reputation: 1336
View
<DataGrid AutoGenerateColumns="False"
EnableRowVirtualization="True"
ItemsSource="{Binding ContributionCollection}">
<!-- DataGrid rows -->
/>
ViewModel
public void Contributions_CommandExecute(object param)
{
ContributionCollection = new ObservableCollection<contribution>(context.contributions.Where(c => c.member == Member));
}
Upvotes: 1
Reputation: 2802
you have to update the list with a Dispatcher so that you don´t interupt the GUI like this:
Dispatcher.BeginInvoke(new Action(() => _contributionCollection = new ObservableCollection<contribution>(queryContributions)));
I had an similar Problem:
Upvotes: 0
Reputation: 35681
this line
return _contributionCollection = new ObservableCollection<contribution>(queryContributions);
updates only value of field. Since it doesn't use property setter, OnPropertyChanged("ContributionCollection");
is not called
the solution is to work with property (by the way, why method is not void?)
// Fired by an ICommand Property
public void Contributions_CommandExecute(object param)
{
var queryContributions = context.contributions.Where(c => c.member == Member);
ContributionCollection = new ObservableCollection<contribution>(queryContributions);
}
Upvotes: 1