c# - MVP after event Binded property null -
i working wpf mvp. have usercontrol databinding. after user interaction (click event) property in presenter null, @ next modify on text when binding happening, customer class not null. think there 2 reference. datacontext of view equal presenter.
the datacontext of view presenter.
public class addcustomerpresenter : presenterbase<addcustomerview> { private customer customer; public customer customer { { return customer ?? new customer(); } set { customer = value; raisepropertychanged(propertyname(() => this.customer)); } } /// <summary> /// todo: view-khoz lehetne irni egy factoryt /// </summary> public addcustomerpresenter() { base.view = new addcustomerview { datacontext = this}; view.save += view_save; } void view_save(object sender, eventargs e) { int = 2; } public void addtocustomers() { new unitofwork().customerrepository.add(customer); } } public partial class addcustomerview : usercontrol { public event eventhandler save; public addcustomerpresenter presenter { { return (addcustomerpresenter)datacontext; } } public addcustomerview() { initializecomponent(); } private void save_click(object sender, routedeventargs e) { var handler = save; if (handler != null) handler(this, eventargs.empty); } } public class customer : notifier { private string name; public string name { { return name; } set { name = value; raisepropertychanged(propertyname(() => this.name)); } } address address; public address address { { return address??new address(); } set { address = value; raisepropertychanged(propertyname(() => this.address)); } } string phonenumber; public string phonenumber { { return phonenumber; } set { phonenumber = value; raisepropertychanged(propertyname(() => this.address)); } } }
<usercontrol x:class="rentacar.views.addcustomerview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d"> <border borderbrush="green" borderthickness="2"> <dockpanel horizontalalignment="center"> <label horizontalalignment="center" content="asdf" dockpanel.dock="top" fontsize="34" /> <grid datacontext="{binding customer}" dockpanel.dock="top"> <grid.resources> <style targettype="label"> <setter property="fontsize" value="12" /> </style> <style targettype="textbox"> <setter property="width" value="112" /> <setter property="horizontalalignment" value="center" /> </style> <style targettype="rowdefinition"> <setter property="height" value="auto" /> </style> </grid.resources> <grid.rowdefinitions> <rowdefinition /> <rowdefinition /> <rowdefinition /> <rowdefinition /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="auto" /> <columndefinition /> </grid.columndefinitions> <label content=":" /> <textbox x:name="asdf" grid.column="1" text="{binding name}" /> <groupbox grid.row="2" grid.columnspan="2" header=""> <grid datacontext="{binding address}"> <grid.rowdefinitions> <rowdefinition /> <rowdefinition /> <rowdefinition /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="auto" /> <columndefinition /> </grid.columndefinitions> <label content=":" /> <textbox grid.column="1" text="{binding city}" /> <label grid.row="1" content=":" /> <textbox grid.row="1" grid.column="1" text="{binding street}" /> <label grid.row="2" content=":" /> <textbox grid.row="2" grid.column="1" text="{binding streetnumber}" /> </grid> </groupbox> <label grid.row="3" content=":" /> <textbox grid.row="3" grid.column="1" text="{binding phonenumber}" /> </grid> <button width="auto" margin="0 10 10 10" horizontalalignment="right" click="save_click" content="" /> </dockpanel> </border> </usercontrol>
demostation:
problem in customer property getter.
return customer ?? new customer();
it means:
if(customer != null) { return customer; } else { return new customer(); }
untill set customer field new customer();
every time.
but want this.
if(customer != null) { return customer; } else { customer = new customer(); return customer; }
or can set field :) example in constructor of addcustomerpresenter
, it's not necessary have getter complicated.
it be:
public customer customer { { return customer; } set { customer = value; raisepropertychanged(propertyname(() => this.customer)); } }
Comments
Post a Comment