INotifyPropertyChanged和InotfyCollectionChanged实现

时间:2018-11-30 15:01:59

标签: c# wpf mvvm inotifypropertychanged inotifycollectionchanged

我正在使用WPF和C#构建Windows桌面应用程序。我正在尝试使用MVVM模型,但无法使用该模型以及INotifyPropertyChangedINotifyCollectionChanged接口。

我正在详细说明我的模型的以下结构,已简化它以提出我的问题。一类模型。我从外部系统获取交易并将其插入数据库中。将它们插入数据库后,我希望显示交易的页面更新为新交易。

我在哪里以及如何实现INotifyPropertyChangedINotifyCollectionChanged接口?我的模型看起来类似于Page-> View-> ViewModel <-Model。

如果不清楚,我可以提供更多信息。

// ---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>

0 个答案:

没有答案