C# & WPF Data Binding to ObservableCollection -
i came linux heavy environment, wrote of tools in python in windows heavy environment, , need share tools team , need gui driven trying learn c#/wpf. i'm getting confused on data binding observablecollection in code behind. can work, don't understand why, bothers me.
my code simple, , literally trying basics working can move on more complicated parts:
xaml:
<listview x:name="lvurllist" horizontalalignment="left" height="441" margin="15,62,0,0" verticalalignment="top" width="486" selectionchanged="listview_selectionchanged" itemssource="{binding path=urllist, elementname=mainwindow1}"> <listview.itemtemplate> <datatemplate> <wrappanel> <textblock text="{binding domain}"/> </wrappanel> </datatemplate> </listview.itemtemplate> </listview>
code behind:
namespace referalscraper { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { //public observablecollection<url> urllist { get; set; } // method 1 public observablecollection<url> urllist = new observablecollection<url>(); // method 2 public mainwindow() { // urllist = new observablecollection<url>(); // method 1 initializecomponent(); urllist.add(new url() { domain = "www.test1.com" }); } public class url { public string domain { get; set; } } private void button_click(object sender, routedeventargs e) { urllist.add(new url() { domain = "www.test2.com" }); urllist.add(new url() { domain = "www.test3.com" }); } } }
the uncommented method creating , instantiating observablecollection doesn't work, code compiles output error:
system.windows.data error: 40 : bindingexpression path error: 'urllist' property not found on 'object' ''mainwindow' (name='mainwindow1')'. bindingexpression:path=urllist; dataitem='mainwindow' (name='mainwindow1'); target element 'listview' (name='lvurllist'); target property 'itemssource' (type 'ienumerable')
which understand means can't find urllist object in mainwindow. don't understand why can't find it.
if switch method 1 , uncomment following 2 lines (and comment out "method 2" part) works fine:
public observablecollection<url> urllist { get; set; } ... public mainwindow(){ urllist = new observablecollection<url>()
why declaring observerablecollection { get; set }
needed? don't quite grasp why can't instantiate observablecollection empty observerablecollection in method 2.
i'm feeling incredibly dense, , haven't quite been able track down right terminology close answering questions. can explain not bright fellow missing?
i have feeling c# understanding missing.
the { get; set; }
syntax defines urilist
property (an auto-implemented property, in case ). without this, urilist
field.
wpf data binding cannot bind fields. see this related question further discussion why case.
generally in c# fields not exposed public, properties preferred. allows change get/set implementation if required. aside, naming convention properties pascalcased (so urilist
rather urilist
).
Comments
Post a Comment