Monday, 15 March 2010

WPF User form TextBoxes binding to a collection -



WPF User form TextBoxes binding to a collection -

i have client form populated textboxes (firstname, lastname, etc) binding property class personname implements inotifypropertychanged. in addition, there 'add record' button when pressed add together record populated user observablecollection

i instantiating personname property object in xaml this:

<local:personname x:key="person" />

and binding textboxes this:

<label style="{staticresource label}" horizontalalignment="right" verticalalignment="center" content="first name" name="firstnamelabel" margin="0,0,15,0" grid.row="1" /> <textbox text="{binding source={staticresource person}, path=firstname, mode=twoway, updatesourcetrigger=propertychanged, notifyonsourceupdated=true, notifyontargetupdated=true}" />

my property implemented as:

public class namelist inherits observablecollection(of personname) ' methods public sub new() mybase.add(new personname("willa", "cather")) mybase.add(new personname("isak", "dinesen")) mybase.add(new personname("victor", "hugo")) mybase.add(new personname("jules", "verne")) end sub end class public class personname implements inotifypropertychanged public event propertychanged propertychangedeventhandler implements inotifypropertychanged.propertychanged ' methods public sub new(byval first string, byval lastly string) me._firstname = first me._lastname = lastly end sub public sub new() end sub ' properties public property firstname() string homecoming me._firstname end set(byval value string) me._firstname = value raiseevent propertychanged(me, new propertychangedeventargs("firstname")) end set end property public property lastname() string homecoming me._lastname end set(byval value string) me._lastname = value raiseevent propertychanged(me, new propertychangedeventargs("lastname")) end set end property ' fields private _firstname string private _lastname string end class

i add together nowadays record in add_record_button click event :

private sub addrecordbutton_click(byval sender system.object, byval e system.windows.routedeventargs) handles addrecordbutton.click dim name jewellery.namelist = ctype(me.findresource("namelist"), jewellery.namelist) ctype(me.findresource("namelist"), jewellery.namelist).add(ctype(me.findresource("person"), jewellery.personname))

this adds , form cleared. when next record added, first record still there overwritten sec record's details in collection. advice? thanks.

wpf model-binding

No comments:

Post a Comment