I am building a Windows desktop application using WPF and C#. I am trying to use the MVVM model but am not able to get my head around with the model and the INotifyPropertyChanged
and INotifyCollectionChanged
interfaces.
I am detailing below structure of my model, have simplified it to put my question across. One class model. I am getting trades from an external system and inserting them in the database. Once I inserted them in the database, I would like the page showing the trades getting updated with the new trades.
Where and how should I implement the INotifyPropertyChanged
and INotifyCollectionChanged
interfaces? My model looks something like Page-->View-->ViewModel<--Model.
If this is not clear then I can provide more information.
// ---Model--
public class Trade
{
private decimal tradeid;
private decimal securityid;
public Trade () {}
// <<public get-set properties for the above 2 items>>
}
// ---ViewModel--
public class TradeVM
{
private ObservableCollection<Trade> _tradeList;
public ObservableCollection<Trades> TradeList
{
get
{
//getting all rows from database
_tradeList = new ObservableCollection<TradesDB>(GetAllRows<Trades>(typeof(Trades)).ToList());
}
set
{
_tradeList = value;
}
}
public TradesVM() {}
}
// ---View---
public class TradeView
{
private TradeVM _tradeVM;
public TradeView()
{
_tradeVM = new TradeVM;
}
public void GetTradesFromExternalSystem()
{
//call the webservice of externsal system and get the trades and insert them in the database.
}
public Ilist<Trade> GetTradesFromDatabase()
{
return _tradeVM.TradeList.ToList();
}
}
// ---Page Class displaying Trades to user---
public partial class TradeData : Page
{
private TradeView _tradeView;
public TradeData()
{
InitializeComponent();
_tradeView = new TradeView();
//datagrid datacontext assignment
this.dgTrades.Datacontext = _tradeView.GetTradesFromDatabase().ToList();
}
private void cmdGetTrades_Click(object sender, RoutedEventArgs e)
{
_tradeView.GetTradesFromExternalSystem();
}
}
// ---Page---
// --just putting the datagrid population line. rest all is default
<igDP:XamDataGrid Name="dgTrades" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" DataSource="{Binding}" MaxHeight="800"></igDP>