c# - Update bound property in ItemsControl -


quick description of setup:

disclaimer: code show want do. command binding instance done event triggers etc. i'm pretty sure wouldn't build, didn't want waste space.

my view:

<itemscontrol itemssource="{binding items}">    <itemscontrol.itemtemplate>        <datatemplate>            <stackpanel>                <textblock text="{binding isfavorite, converter={staticresource favoriteconverter}" tap="{binding favoritecommand, commandparameter={binding}}"/>                <textblock text="{binding title}"/>             </stackpanel>         </datatemplate>     </itemscontrol.itemtemplate> </itemscontrol> 

my viewmodel:

public class viewmodel : viewmodelbase {     public ilist<item> items { get; set; }     public relaycommand<item> favorite     {               {            return new relaycommand<item>(item =>                 {                     item.isfavorite = !item.isfavorite;                     raisepropertychanged(string.empty);                 };         }      } } 

my model:

public class item {     public string title { get; set; }     public bool isfavorite { get; set; } } 

*my question:*

how can isfavorite-property update without implementing inotifypropertychanged? it's model class, , wouldn't creating viewmodel sole purpose of updating 1 property. thought calling propertychanged empty string update everything, alas didn't.

*my solution:*

public class itemviewmodel : viewmodelbase {     public item model { get; private set; }     public bool isfavorite     {         { return model.isfavorite; }         set         {             model.isfavorite = value;             raisepropertychanged("isfavorite");         }     } } 

if binding value , expect changing @ runtime, should implement inotifypropertychanged since wpf's binding system uses interface know when needs update.

but said, might able away raising propertychange notification entire items collection, although i'm not entirely sure work because actual collection hasn't changed, property of 1 of items inside collection. , wpf knows not bother re-evaluating property if doesn't change.

raisepropertychanged("items"); 

if doesn't work, remove item , re-add trigger collectionchange notification, might not work , may cause other problems depending on application design.

// may have exact syntax wrong here var index = items.indexof(item); var tmp = items[index]; items.remove(tmp); items.add(tmp, index); 

but best implement inotifypropertychanged on item class.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -