Bind List<Object> to a comboxbox in c# wpf -


i have static class named building contains list<beam> beams property;

public static class building {     public static readonly list<beam> beams = new list<beam>(); }  public class beam {     public string story;     public double elevation; } 

i'm trying bind building.beams combobox in xaml elevation , story properties of each item in building.beams list displayed in different columns in combobox. have been able implement 2 columns, can't bind these properties.

here have tried far:

<combobox x:name="cmbbuilding"  itemssource="{binding}">     <combobox.itemtemplate>         <datatemplate>             <grid width="300">                 <textblock width="150"  text="{binding path=story }"/>                 <textblock width="150" text="{binding path=elevation}"/>             </grid>         </datatemplate>     </combobox.itemtemplate> </combobox>  var b1 = new beam { elevation = 320, story = "st1" }; var b2 = new beam { elevation = 640, story = "st2" }; building.beams.add(b1); building.beams.add(b2); 

try example:

xaml

<grid>     <combobox x:name="cmbbuilding" width="100" height="25" itemssource="{binding path=beams}">         <combobox.itemtemplate>             <datatemplate>                 <grid width="300">                     <textblock width="150" text="{binding path=story}" horizontalalignment="left" />                     <textblock width="150" text="{binding path=elevation}" horizontalalignment="right" />                 </grid>             </datatemplate>         </combobox.itemtemplate>     </combobox>      <button content="add item" verticalalignment="top" click="button_click" /> </grid> 

code-behind

public partial class mainwindow : window {     building building = new building();      public mainwindow()     {         initializecomponent();          building.beams = new list<beam>();          building.beams.add(new beam                             {                                 elevation = 320,                                 story = "st1"                             });          this.datacontext = building;     }      private void button_click(object sender, routedeventargs e)     {         var b1 = new beam { elevation = 320, story = "st1" };         var b2 = new beam { elevation = 640, story = "st2" };          building.beams.add(b1);         building.beams.add(b2);          cmbbuilding.items.refresh();     } }  public class building {     public list<beam> beams     {         get;         set;     } }  public class beam {     public string story      {         get;         set;     }      public double elevation     {         get;         set;     } } 

some notes

  • when use properties in binding, need properties , set, not fields.

  • properties, added list<t> automatically update, should call mycombobox.items.refresh() method, or use observablecollection<t>:

observablecollection represents dynamic data collection provides notifications when items added, removed, or when whole list refreshed.


Comments

Popular posts from this blog

Change the color of an oval at click in Java AWT -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -