WPF DataContext does not refresh the DataGrid using MVVM model
Posted
by vikram bhatia
on Stack Overflow
See other posts from Stack Overflow
or by vikram bhatia
Published on 2009-05-25T17:46:23Z
Indexed on
2010/04/16
15:33 UTC
Read the original article
Hit count: 1201
Project Overview
I have a view which binds to a viewmodel containing 2 ObserverableCollection. The viewmodel constructor populates the first ObserverableCollection and the view datacontext is collected to bind to it through a public property called Sites.
Later the 2ed ObserverableCollection is populated in the LoadOrders method and the public property LoadFraudResults is updated for binding it with datacontext.
I am using WCF to pull the data from the database and its getting pulled very nicely.
VIEWMODEL SOURCE
class ManageFraudOrderViewModel:ViewModelBase
{
#region Fields
private readonly ICollectionView collectionViewSites;
private readonly ICollectionView collectionView;
private ObservableCollection<GeneralAdminService.Website> _sites;
private ObservableCollection<FraudService.OrderQueue> _LoadFraudResults;
#endregion
#region Properties
public ObservableCollection<GeneralAdminService.Website> Sites
{
get { return this._sites; }
}
public ObservableCollection<FraudService.OrderQueue> LoadFraudResults
{
get { return this._LoadFraudResults;}
}
#endregion
public ManageFraudOrderViewModel()
{
//Get values from wfc service model
GeneralAdminService.GeneralAdminServiceClient generalAdminServiceClient = new GeneralAdminServiceClient();
GeneralAdminService.Website[] websites = generalAdminServiceClient.GetWebsites();
//Get values from wfc service model
if (websites.Length > 0)
{
_sites = new ObservableCollection<Wqn.Administration.UI.GeneralAdminService.Website>();
foreach (GeneralAdminService.Website website in websites)
{
_sites.Add((Wqn.Administration.UI.GeneralAdminService.Website)website);
}
this.collectionViewSites= CollectionViewSource.GetDefaultView(this._sites);
}
generalAdminServiceClient.Close();
}
public void LoadOrders(Wqn.Administration.UI.FraudService.Website website)
{
//Get values from wfc service model
FraudServiceClient fraudServiceClient = new FraudServiceClient();
FraudService.OrderQueue[] OrderQueue = fraudServiceClient.GetFraudOrders(website);
//Get values from wfc service model
if (OrderQueue.Length > 0)
{
_LoadFraudResults = new ObservableCollection<Wqn.Administration.UI.FraudService.OrderQueue>();
foreach (FraudService.OrderQueue orderQueue in OrderQueue)
{
_LoadFraudResults.Add(orderQueue);
}
}
this.collectionViewSites= CollectionViewSource.GetDefaultView(this._LoadFraudResults);
fraudServiceClient.Close();
}
}
VIEW SOURCE
public partial class OrderQueueControl : UserControl {
private ManageFraudOrderViewModel manageFraudOrderViewModel ;
private OrderQueue orderQueue;
private ButtonAction ButtonAction;
private DispatcherTimer dispatcherTimer;
public OrderQueueControl()
{
LoadOrderQueueForm();
}
#region LoadOrderQueueForm
private void LoadOrderQueueForm()
{
//for binding the first observablecollection
manageFraudOrderViewModel = new ManageFraudOrderViewModel();
this.DataContext = manageFraudOrderViewModel;
}
#endregion
private void cmbWebsite_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
BindItemsSource();
}
#region BindItemsSource
private void BindItemsSource()
{
using (OverrideCursor cursor = new OverrideCursor(Cursors.Wait))
{
if (!string.IsNullOrEmpty(Convert.ToString(cmbWebsite.SelectedItem)))
{
Wqn.Administration.UI.FraudService.Website website = (Wqn.Administration.UI.FraudService.Website)Enum.Parse(typeof(Wqn.Administration.UI.FraudService.Website),cmbWebsite.SelectedItem.ToString());
//for binding the second observablecollection*******
manageFraudOrderViewModel.LoadOrders(website);
this.DataContext = manageFraudOrderViewModel;
//for binding the second observablecollection*******
}
}
}
#endregion
}
XAML
ComboBox x:Name="cmbWebsite" ItemsSource="{Binding Sites}" Margin="5" Width="100" Height="25" SelectionChanged="cmbWebsite_SelectionChanged"
DataGrid ItemsSource ={Binding Path = LoadFraudResults}
PROBLEM AREA:
When I call the LoadOrderQueueForm to bind the first observablecollection and later BindItemsSource to bind 2ed observable collection, everything works fine and no problem for the first time binding.
But, when I call BindItemsSource again to repopulate the obseravablecollection based on changed selected combo value via cmbWebsite_SelectionChanged, the observalblecollection gets populated with new value and LoadFraudResults property in viewmodule is populated with new values; but when i call the datacontext to rebind the datagrid,the datagrid does not reflect the changed values.
In other words the datagrid doesnot get changed when the datacontext is called the 2ed time in BindItemsSource method of the view.
manageFraudOrderViewModel.LoadOrders(website);
this.DataContext = manageFraudOrderViewModel;
manageFraudOrderViewModel values are correct but the datagrid is not relected with changed values.
Please help as I am stuck with this thing for past 2 days and the deadline is approaching near.
Thanks in advance
© Stack Overflow or respective owner