c# - Why isn't this data binding working? -
so i'm brand new wpf data binding, , is.. complicated. @ point, i'm trying create list of premade test items , have displayed in listbox data template when press button. after hours of puzzling through tutorials , msdn best come with.
the data item want make list from:
class listingitem {     private string title;     private string user;     private string category;      //dummy constructor test purposes     public listingitem()     {         title = "testtitle";         user = "testuser";         category = "testcatagory";     } } the quick , dirty list creator:
class listmaker {     public static list<listingitem> getlisting()     {         list<listingitem> listing = new list<listingitem>();         for(int = 0; <100; i++)         {             listing.add(new listingitem());         }         return listing;     } } the xaml of list itself:
<listbox x:name="listing"> <listbox.itemtemplate>     <datatemplate>         <stackpanel orientation="vertical">             <stackpanel orientation="horizontal">                 <textblock foreground="gray" margin="25,0,0,0" text="{binding user}"/>                 <textblock foreground="gray" margin="25,0,0,0" text="{binding category}"/>             </stackpanel>             <textblock foreground="black" width="270" textwrapping="wrap" text="{binding title}"/>         </stackpanel>     </datatemplate> </listbox.itemtemplate> and finally, button click event supposed make magic happen:
private void tabclickevent(object sender, routedeventargs e)     {         listing.datacontext = redditscanner.getlisting();     } problem is, obviously, magic not happening. no errors or easy, press button , dont see change list box. this?
you cannot bind private fields. not public fields think.
use properties:
class listingitem {     //private string title;     //private string user;     //private string category;      public string title { get; set; }     public string user { get; set; }     public string category { get; set; }      //dummy constructor test purposes     public listingitem()     {         title = "testtitle";         user = "testuser";         category = "testcatagory";     } } and full databinding have to implement inotifypropertychanged on listingitem.
the magic not happening. no errors or easy,
keep eye on output window during execution. binding errors reported.
Comments
Post a Comment