How to bind data correctly to a ListBox so it would update [C#/WPF/MVVM] -


i'm stuck trouble updating bound data in wpf. basically, i've got data in app, doesn't update when properties change , can't fix it.

in app i'm trying use mvvm pattern, there model properties x, y:

public class Сross : inotifypropertychanged {     private double x;     private double y;      public double x     {         { return x; }         set         {             x = value;             raisepropertychanged("x");         }     }      public double y     {         { return y; }         set         {             y = value;             raisepropertychanged("y");         }     }      public event propertychangedeventhandler propertychanged;      protected virtual void raisepropertychanged(string propertyname)     {         if (propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(propertyname));         }     } } 

...there viewmodel, contains observablecollection of crosses:

public class mainwindowviewmodel : inotifypropertychanged {     public event propertychangedeventhandler propertychanged;      crosses = new observablecollection<cross>()     {         new cross ()         {             x = 300,             y = 200         },         new cross ()         {             x = 400,             y = 300         }     };      private observablecollection<cross> crosses;     public observablecollection<cross> crosses     {                 {             return crosses;         }         set         {             crosses = value;             raisepropertychanged("crosses");         }     }      private void raisepropertychanged(string propertyname)     {         if (propertychanged != null)             propertychanged(this, new propertychangedeventargs(propertyname));     } } 

...and there view, there listbox, databound observablecollection of crosses.

<page x:class="charmaker.app.mainpageview">     <page.datacontext>          <local:mainwindowviewmodel />     </page.datacontext>      ....      <listbox>         <listbox.itemspanel>             <itemspaneltemplate>                 <canvas isitemshost="true" background="#01ffffff" />             </itemspaneltemplate>         </listbox.itemspanel>          <listbox.itemssource>              <binding path="crosses" mode="twoway" />         </listbox.itemssource>          <listbox.itemcontainerstyle>              <style targettype="listboxitem">                   <setter property="canvas.left" value="{binding x, mode=twoway}" />                   <setter property="canvas.top" value="{binding y, mode=twoway}" />              </style>          </listbox.itemcontainerstyle>     </listbox> </page> 

this code works when move cross item (which has datatemplate based on thumb control) in listbox field, it's canvas.left(or top) changing , updates x , y properties of cross item, , other way around.

  <datatemplate datatype="{x:type model:cross}">                             <thumb dragdelta="thumb_dragdelta"                            isenabled="{binding isselected,relativesource={relativesource findancestor, ancestortype={x:type listboxitem}}}">                                 <thumb.template>                                     <controltemplate targettype="thumb">                                         <canvas margin="0">                                             <line x1="-5" x2="5" y1="-5" y2="5"  stroke="#ff2e61ab" strokethickness="1.5"                                               x:name="firstline" />                                              <line x1="-5" x2="5" y1="5" y2="-5"  stroke="#ff2e61ab" strokethickness="1.5"                                               x:name="secondline" />                                         </canvas>                                         <controltemplate.triggers>                                             <datatrigger binding="{binding isselected, relativesource={relativesource findancestor, ancestortype={x:type listboxitem}}}" value="true">                                                 <setter targetname="firstline" property="stroke" value="red"/>                                                 <setter targetname="secondline" property="stroke" value="red"/>                                             </datatrigger>                                         </controltemplate.triggers>                                     </controltemplate>                                 </thumb.template>                             </thumb>                         </datatemplate> 

the problem after listbox loaded (and crosses appear on screen in appropriate positions), don't change x , y properies in viewmodel move them. , if change x , y in observablecollection crosses, don't move on screen. it's worth mentioning, did listbox textboxes testing, bound x, y properties of crosses collection:

<listbox grid.row="2" grid.columnspan="4" itemssource="{binding crosses}">     <listbox.resources>         <datatemplate datatype="{x:type model:cross}">             <grid>                 <grid.columndefinitions>                     <columndefinition width="1*"/>                     <columndefinition width="1*"/>                 </grid.columndefinitions>                  <textbox grid.column="0" text="{binding x, mode=twoway}" />                 <textbox grid.column="1" text="{binding y, mode=twoway}" />              </grid>         </datatemplate>     </listbox.resources> </listbox> 

and funny thing in listbox data binding works fine! values changing in textboxes when move crosses, , crosses move positions when value in textbox changed.

but in same time (if pause app) see values in observable collection crosses in viewmodel same when loaded first time.

please, me figure out, problem is! thank in advance!

after lots of hours debugging this, seems, found problem.

i had page datacontext set mainviewmodel, , page child of mainwindow, had datacontext property set mainviewmodel. didn't realize, both page , mainwindow created mainviewmodel instance of own. had 2 instances , while 1 working , updating should, confusing me.

stupid mistake, know. anyway trying help.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -