c# - Binding a ComboBox to part of an ObservableCollection -


i have wpf application in c# have object of class mycollection extends observablecollection<mytype> holds items purpose of binding them several comboboxes.

however, every combobox must display subset of collection (based on property of elements), , may change based on user input.

how can obtain behavior keeping every subset updated data original collection? there known design pattern scenario?


edit: since formulation of question misunderstood, here's example. have observablecollection<person> object, class person has age , name properties. have 3 combo boxes, first 2 must display name of person objects odd age, while third must of age. roles might change @ runtime (e.g. first , last has display odd ages, second ages) if person objects added or deleted collection, changes must reflected on corresponding comboboxes. name , age properties may considered constant.

if understand question correctly, need sort of filtering mechanism.

take @ icollectionview interface , implementations such collectionviewsource might achieve this.

you need handle filter event implements filtering logic.

here class @ msdn (http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource(v=vs.110).aspx)

an example:

container class:

public string name { get; set; } public string capital { get; set; }  public country(string name, string capital) {     this.name = name;     this.capital = capital; } 

model class:

private observablecollection<country> _countries; private icollectionview _european; private icollectionview _american;  public observablecollection<country> countries {     {         if (_countries == null) {             _countries = new observablecollection<country>();         }          return _countries;     } }  public icollectionview european {     {         if (_european == null) {             _european = new collectionviewsource {                 source = this.countries             }.view;             _european.filter += (e) => {                 country c = e country;                 if (c.name == "uk" || c.name == "ireland" || c.name == "france") {                     return true;                 }                  return false;             };         }          return _european;     } }  public icollectionview american {     {         if (_american == null) {             _american = new collectionviewsource {                 source = this.countries             }.view;             _american.filter += (e) => {                 country c = e country;                 if (c.name == "usa" || c.name == "canada" || c.name == "mexico") {                     return true;                 }                  return false;             };         }          return _american;     } } 

initialization code:

private model _model;  public model model {     {         if (_model == null) {             _model = new model();         }          return _model;     } }  public mainwindow() {     initializecomponent();     this.datacontext = this.model;     this.model.countries.add(new country("uk", "london"));     this.model.countries.add(new country("ireland", "dublin"));     this.model.countries.add(new country("france", "paris"));     this.model.countries.add(new country("usa", "washington d. c."));     this.model.countries.add(new country("mexico", "mexico city"));     this.model.countries.add(new country("canada", "ottawa")); } 

xaml:

<stackpanel>     <combobox         itemssource='{binding path=european}'>         <combobox.itemtemplate>             <datatemplate>                 <stackpanel                     orientation='horizontal'>                     <textblock                         text='{binding path=name}' />                     <textblock                         text=', ' />                     <textblock                         text='{binding path=capital}' />                 </stackpanel>             </datatemplate>         </combobox.itemtemplate>     </combobox>      <combobox         itemssource='{binding path=american}'>         <combobox.itemtemplate>             <datatemplate>                 <stackpanel                     orientation='horizontal'>                     <textblock                         text='{binding path=name}' />                     <textblock                         text=', ' />                     <textblock                         text='{binding path=capital}' />                 </stackpanel>             </datatemplate>         </combobox.itemtemplate>     </combobox> </stackpanel> 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -