3

I'm new to WPF and MVVM and I started with "Jason Dolinger on Model-View-ViewModel" article and example but I have some questions regarding databinding.

1) In his demo application, he's subclassing DependencyObject for the ObservableCollection Items. What are the pros/cons compared to INotifyPropertyChanged ?

2) What is the best way to update the view from the model in a datagrid/listview ? In his example, he registers as a listener when a Quote object is added or updated :

_source.QuoteArrived += new Action<Quote>(_source_QuoteArrived);

Than the ViewModel creates and adds the QuoteViewModel object to the collection OR updates the view by setting the updated Quote object in the convenient QuoteViewModel object using a dictionary called _quoteMap.

void _source_QuoteArrived(Quote quote)
{

    QuoteViewModel qvm;
    if (_quoteMap.TryGetValue(quote.Symbol, out qvm))
    {
        qvm.Quote = quote;
    }
    else
    {
        qvm = new QuoteViewModel();
        qvm.Quote = quote;

        this.Quotes.Add(qvm);

        _quoteMap.Add(quote.Symbol, qvm);
    }
}   

Is there a better way to update the view from the model when a Quote object has been updated or am I forced to create a dictionnary ? It would be so easier if the listview could be updated immediately when a Quote object is updated... without having Quote to subclass INotifyPropertyChanged or DependencyObject.

Thanks

4

1 回答 1

1

对于您的第一个问题,请参阅此 StackOverflow 问题。一般来说,人们似乎更喜欢 INotifyPropertyChanged.

至于您的第二个问题,鉴于报价可以随时到达,您需要某种方法将到达的报价映射到您收藏中已有的报价。使用字典似乎是一种明智的做法。你还有什么建议?

您说 ListView 立即更新会很好,但是 ListView 怎么知道新的 Quote 对应的对象是什么?ListView 纯粹监视一个实现的集合INotifyCollectionChanged,它对内部结构一无所知Quote,或者Quote.Symbol

于 2012-04-23T08:10:04.740 回答