wpf - observableCollection: how to establish type in runtime? -


i have datagrid, uses observablecollection bind itemssource property (mvvm pattern). in viewmodel have property observablecollection (mycollection). however, datagrid can show information of 2 different types, decide in runtime.

normally, use the observablecollection in away:

observablecollection<mytype> mycollection = new observablecollection<mytype>(); 

but now, have string parameter me type need, that:

if(parameter == "typea") {     mycollection = new observablecollection<typea>(); }  if(parameter == "typeb") {     mycollection = new observablecollection<typeb>(); } 

is possible that?

if make typea , typeb derived common base class or interface, can keep same collection.

but if want 2 different collections, you can raise collection property notify changed type.

ienumerable mycollection {         {         if(currenttype == typeof(typeb)             return mytypebcollection;         else if(currenttype == typeof(typea)             return mytypeacollection;     } } 

so bind mycollection in view itemssource, , raise property has changed.

remember might need different datatemplate, datatemplateselector might come in handy.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -