c# - Force Binding isn't what I want -
my oneway binding isn't working i'm expecting.
when click button add new "person" doesn't add newly entered person listview until shut down , restart application (so value gets added db not ui)
what doing incorrectly? have inotifypropertychanged, have observablecollection... missing?
i have model:
public class person : inotifypropertychanged { public event propertychangedeventhandler propertychanged; private string _personname; private string _personemail; private string _personphone; private datetime _persondob; [primarykey, autoincrement] public int personid { get; set; } [maxlength(25)] public string personname { { return _personname; } set { _personname = value; onpropertychanged("personname"); } } [maxlength(50)] public string personemail { { return _personemail; } set { _personemail = value; onpropertychanged("personemail"); } } [maxlength(13)] public string personphone { { return _personphone; } set { _personphone = value; onpropertychanged("personphone"); } } public datetime persondob { { return _persondob;} set { _persondob = value; onpropertychanged("persondob"); } } public boolean ispersonactive { get; set; } public string summary { { return string.format("{0} - {1} : {2} -- {3}", personname, personemail, personphone, persondob); } } // create onpropertychanged method raise event protected void onpropertychanged(string name) { propertychangedeventhandler handler = propertychanged; if (handler != null) { handler(this, new propertychangedeventargs(name)); } } }
in viewmodel have this:
public class choresvm { private observablecollection<win8chores.model.databasetables.person> _personlist; public observablecollection<win8chores.model.databasetables.person> personlist { { return _personlist; } set { _personlist = value; } }
...
public observablecollection<win8chores.model.databasetables.person> selectallperson() { using (var db = new sqliteconnection(dbpath)) { observablecollection<win8chores.model.databasetables.person> plist = new observablecollection<win8chores.model.databasetables.person>(db.query<win8chores.model.databasetables.person>("select personid,personname,personemail,persondob person")); _personlist = new observablecollection<model.databasetables.person>(db.query<win8chores.model.databasetables.person>("select personid,personname,personemail,persondob person")); return _personlist; } } public void insertperson(string name, string email, string phone, datetime dob, boolean isactive = true) { dbpath = path.combine(windows.storage.applicationdata.current.localfolder.path, "mydb"); db = new sqliteconnection(dbpath); using (db) { var p = db.insert(new win8chores.model.databasetables.person() { personname = name, personemail = email, personphone = phone, persondob = dob, ispersonactive = isactive }); } selectallperson(); }
then in view:
public mainpage() { this.initializecomponent(); vm = new viewmodel.choresvm(); datacontext = vm; } private void btnadd_click(object sender, routedeventargs e) { datetime mydob = new datetime(1955, 02, 28); vm.insertperson("test","test@live.com","123-456-7890", mydob); }
with xaml this:
<listview horizontalalignment="left" height="224" margin="287,344,0,0" verticalalignment="top" width="740" x:name="test" displaymemberpath="summary" itemssource="{binding personlist, mode=oneway}" />
try implement inotifypropertychanged on choresvm , raise propertychanged("personlist") when initialize collection (in selectallperson() or personlist setter). way itemssource binding notified, collection property changed , pickup new collection. have typo. in binding have "personlist" (pascal case) property in camel case (personlist)
Comments
Post a Comment