c# - XAML bound property isn't working as expected -


i have crew property, property has several fields, few of code , invoiceamount. plus button supposed insert new crew observablecollection of crews. adding first item works fine, when second item inserted first item's code changes second item , second item has no visible code. how fix new crew inserted every time click + button?

starting ui:

enter image description here

after 1 item (a) has been added:

enter image description here

second item (b) has been added:

enter image description here

here's viewmodel code:

public class mainpageviewmodel : viewmodelbase {     public mainpageviewmodel()     {         addcrewcommand = new customcommand(param => addcrew(), null);         crews.collectionchanged += new notifycollectionchangedeventhandler(crews_updated);     }      private void crews_updated(object sender, notifycollectionchangedeventargs e)     {         raisepropertychanged("lvcrewlist");     }      public crew crew { get; set; } = new crew();      public observablecollection<crew> crews { get; private set; } = new observablecollection<crew>();      public crew selectedcrew { get; set; }      public icommand addcrewcommand { get; private set; }      private void addcrew()     {         crews.add(crew);         crew = new crew();     }      public observablecollection<string> selectedworkorder { get; set; } } 

viewmodelbase:

public class viewmodelbase : inotifypropertychanged {     public event propertychangedeventhandler propertychanged;     protected void raisepropertychanged(string propertyname)     {         propertychanged?.invoke(this, new propertychangedeventargs(propertyname));     } } 

here's xaml bit assigns code field:

        <stackpanel orientation="horizontal" verticalalignment="top" >             <label content="crew" width="55" height="25" margin="10,10,0,0"/>             <textbox x:name="txtcrew" width="75" height="25" margin="0,10,10,0"                 text="{binding crew.code, mode=twoway}" />             <button content="+" width="25" height="25" margin="0, 10, 0, 0" command="{binding addcrewcommand}" />         </stackpanel> 

crew class:

public class crew {     public string code { get; set; }      public decimal invoiceamount { get; set; } = 0;      public job job { get; set; }      public override string tostring() => code; } 

it because not raising propertychanged event crew property, therefore textbox still bound added crew.

change mainpageviewmodel.crew property following:

public class mainpageviewmodel : viewmodelbase {     .............     private crew _crew = new crew();     public crew crew     {         { return _crew; }         set         {             if (_crew == value) return;             _crew = value;             raisepropertychanged(nameof(crew));         }     }     ....... } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -