c# - ObservableCollection is not reflecting UI -


i beginner wpf , mvvm. while coding it, came across issue listitems variable in vehicleaddviewmodel class, of type observablecollection list. binding view viewmodel proper, listitems getting updated, onpropertychanged method working when add new list listitems, not reflecting ui.

at other end, if make listitems object static, starts working. don't know why happening.

i anxious know why happening this.

i have bunch of files, putting relevant ones working , non-working versions, please let me know if need other file.

listofvehicle.xaml

<window x:class="seris.listofvehicle"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             title="listofvehcle" height="600" width="700"             xmlns:l="clr-namespace:seris.viewmodels"             xmlns:cnv="clr-namespace:seris.converters"> <window.resources>     <cnv:id2name x:key="converter" /> </window.resources>  <grid horizontalalignment="center">      <label content="manage vehicle" horizontalalignment="left" height="27" margin="261,8,0,0" verticalalignment="top" width="103" fontweight="bold" fontsize="12"/>     <label content="seris cad" horizontalalignment="left" height="30" margin="53,8,0,0" verticalalignment="top" width="84" fontweight="bold"/>     <menu x:name="listofpersonnnel" horizontalalignment="left" height="32" margin="10,35,0,0" verticalalignment="top" width="603">         <menuitem header="manage vehicle &gt;&gt;" />     </menu>      <button name="add_button" commandparameter="add"  command="{binding openaddwindow_command}"  content="add" height="28" width="81" margin="246,396,315,46"/>     <button name="replace_button" commandparameter="replace" command="{binding replacebutton_command}" isenabled="{binding isenablereplacebutton, updatesourcetrigger=propertychanged, mode=twoway}"  content="replace" height="28" width="81" margin="345,396,216,46"/>     <button name="remove_button" commandparameter="remove" command="{binding removebutton_command}" isenabled="{binding isenablereplacebutton, updatesourcetrigger=propertychanged, mode=twoway}"  content="remove" height="28" width="81" margin="442,396,119,46"/>       <listview name ="grid" margin="104,67,185,226" >         <datagrid name="dg" itemssource="{binding listitems, mode=twoway, updatesourcetrigger=propertychanged}" selecteditem="{binding selectedrow, mode=twoway}"  selectionmode="single" gridlinesvisibility="none" isreadonly="true" autogeneratecolumns="false" borderthickness="0">             <datagrid.datacontext>                 <l:vehicleaddviewmodel />             </datagrid.datacontext>             <datagrid.columns>                 <datagridtextcolumn header="vehicle no" binding="{binding vehicleno}"/>                 <datagridtextcolumn header="model" binding="{binding model}" />                 <datagridtextcolumn header="manufacturingdate" binding="{binding manufacturingdate}" />                 <datagridtextcolumn header="iuno" binding="{binding iuno}" />                 <datagridtextcolumn header="personnel" binding="{binding personnelnameselected, converter={staticresource converter} }" />                 <datagridtextcolumn header="unique no" binding="{binding uniqueno}"/>             </datagrid.columns>         </datagrid>     </listview>     <textbox name="search_text" text="{binding searchstring, mode=onewaytosource, updatesourcetrigger=propertychanged}" horizontalalignment="left" height="23" margin="520,72,0,0" textwrapping="wrap" verticalalignment="top" width="120" />   </grid>  </window> 

listofvehicle.xaml.cs

using seris.viewmodels; using seris.views; using system; using system.collections.generic; using system.linq; using system.text; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes;  namespace seris { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class listofvehicle : window {     public listofvehicle()     {         this.datacontext = vehiclemainviewmodel.getinstance;         this.show();         initializecomponent();     }   } } 

observableobject.cs

using system; using system.collections.generic; using system.componentmodel; using system.diagnostics; using system.linq; using system.text; using system.windows;  namespace seris.models { public abstract class observableobject: inotifypropertychanged, idisposable {     public event propertychangedeventhandler propertychanged;      protected bool onpropertychanged(string propertyname)     {          this.verifypropertyname(propertyname);           propertychangedeventhandler handler = propertychanged;          if(handler!=null)         {             if (propertyname != null)             {                  handler(this, new propertychangedeventargs(propertyname));                 return true;              }         }         return false;     }      public void verifypropertyname(string propertyname)     {         if (typedescriptor.getproperties(this)[propertyname] == null)         {             string msg = "invalid property name: " + propertyname;              if (this.throwoninvalidpropertyname)                 throw new exception(msg);             else                 debug.fail(msg);         }     }      public virtual void dispose()     {         // override method     }      public bool throwoninvalidpropertyname { get; set; } } } 

vehicleaddviewmodel.cs

...      private observablecollection<vehiclemodel> _listitems =  new observablecollection<vehiclemodel>();     public observablecollection<vehiclemodel> listitems     {         { return _listitems; }         set         {             if (value == null || (!value.equals(_listitems)))             {                 _listitems = value;                 onpropertychanged("listitems");             }         }     }     ...       public void savetolist(object o1)     {          try         {             // setting flags             errormessage = "";              //checking duplicate entry              if (isduplicate())             {                 throw (new exception("duplicate entries not allowed"));             }               // adding record              using(vm = new vehiclemodel(true,vehicleno, model, manufacturingdate, iuno, personnelnameselected))             {                   // keeping record of selected row                  int indextoremove;                  if (selectedrow != null && replaceflag==true)                  {                     replaceflag = false;                      vehiclemodel selectedrecord = listitems.first(x => x.uniqueno == uniqueno);                     indextoremove = listitems.indexof(selectedrecord);                      listitems.insert(indextoremove, new vehiclemodel(vm));                      listitems.removeat(indextoremove + 1);                 }                 else                     listitems.add(new vehiclemodel(vm));                }             vehiclemainviewmodel.getinstance.closeadd();              // clearing form              clearalldata();          }         catch (exception ex)         {             errormessage = ex.message;         }     }     ...      public vehicleaddviewmodel()     {          // setting flags         errormessage = "";         isenablereplacebutton = false;          // commands initialization         try         {             savebutton_command = new relaycommand(new action<object>(savetolist));          }         catch(exception ex)         {             showmessage("exception occured in command variables");         }      }       #endregion   } } 

vehiclemainviewmodel

{ public class vehiclemainviewmodel: observableobject {       ...      public vehiclemainviewmodel()     {         //vehiclemainviewmodel.listitems = new observablecollection<vehiclemodel>();         //vehiclemainviewmodel.personnelobject = personnelmainviewmodel.getinstance._personnel_list;          try         {             replacebutton_command = new relaycommand(new action<object>(replacetolist));             removebutton_command = new relaycommand(new action<object>(removelist));             openaddwindow_command = new relaycommand(new action<object>(openadd));         }         catch (exception ex)         {             vehicleaddviewmodel.showmessage("exception occured in command variables");         }     } } } 

"now changing 2 files vehicleaddviewmodel , vehiclemainviewmodel below, making listview object static, , starts working..."

vehicleaddviewmodel

public class vehicleaddviewmodel : observableobject {  . . .     private static observablecollection<vehiclemodel> _listitems = new observablecollection<vehiclemodel>();     public static observablecollection<vehiclemodel> listitems     {         { return _listitems; }         set         {             if (value == null || (!value.equals(_listitems)))             {                 _listitems = value;             }         }     }      #region methods      private vehiclemodel vm;       public void savetolist(object o1)     {          try         {             // setting flags             errormessage = "";              //checking duplicate entry              if (isduplicate())             {                 throw (new exception("duplicate entries not allowed"));             }               // adding record              using(vm = new vehiclemodel(true,vehicleno, model, manufacturingdate, iuno, personnelnameselected))             {                   // keeping record of selected row                  int indextoremove;                  if (selectedrow != null && replaceflag==true)                  {                     replaceflag = false;                      vehiclemodel selectedrecord = listitems.first(x => x.uniqueno == uniqueno);                     indextoremove = listitems.indexof(selectedrecord);                      listitems.insert(indextoremove, new vehiclemodel(vm));                      listitems.removeat(indextoremove + 1);                 }                 else                     listitems.add(new vehiclemodel(vm));                }             vehiclemainviewmodel.getinstance.closeadd();              // clearing form              clearalldata();          }         catch (exception ex)         {             errormessage = ex.message;         }     }       public vehicleaddviewmodel()     {          // setting flags         errormessage = "";         isenablereplacebutton = false;          // commands initialization         try         {             savebutton_command = new relaycommand(new action<object>(savetolist));          }         catch(exception ex)         {             showmessage("exception occured in command variables");         }      }       #endregion   } } 


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

python - Django-cities exits with "killed" -

python - How to get a widget position inside it's layout in Kivy? -