c# - ItemsControl view doesn't updated when an item in ObservableCollection is updated -



c# - ItemsControl view doesn't updated when an item in ObservableCollection is updated -

introduction :

hi, meet weird problem here. itemscontrol doesn't update view if updating model (e.g. changing value of isselected).

one of isselected purpose however, is, if value true, background of musicalnotationbox (usercontrol) changed blue, , if it's false it's changed transparent.

a lot of person asked : why not using trigger such as focus for isselected? because it's not "visual" purposes. have command alter property (for illustration note's octave) of each musicalnotation object in vm's musicalnotations, isselected==true (support multiselection), think need isselected in model. but not problem here, please no reply solely on matter.

the problem :

the model property changed (checked , verified), seems the view isn't. if utilize singular form of usercontrol, e.g. <c:musicalnotationbox datacontext={binding}/> (ofc along it's friends a.k.a right properties in vm), synced perfectly. so, i'm not quite sure problem lies oc. update : if i, illustration create mousebinding each time click, new musicalnotation added list, then, the view updated.

i have read several topic (google , here) on "observable collection doesn't update itemscontrol`, still found no satisfying answer.

here's code (code may trimmed (...) clarity sake) :

model

public class musicalnotation : ... inotifypropertychanged { public event propertychangedeventhandler propertychanged; ... private bool _isselected; ... public bool isselected { { homecoming _isselected; } set { _isselected = value; notifypropertychanged("isselected"); } } ... public musicalnotation() { ... isselected = false; } ... private void notifypropertychanged(string propertyname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } }

view model

public class mainwindowmodelview : inotifypropertychanged { public event propertychangedeventhandler propertychanged; private observablecollection<musicalnotation> _musicalnotations; ... public observablecollection<musicalnotation> musicalnotations { { homecoming _musicalnotations; } set { _musicalnotations = value; notifypropertychanged("musicalnotations"); } } public mainwindowmodelview() { ... musicalnotations = new observablecollection<musicalnotation>(); //direct initialization testing purpose musicalnotations.add(musicalnotation.getemptynote(new timesignature(4, 4))); musicalnotations.add(musicalnotation.getemptynote(new timesignature(4, 4))); foreach (musicalnotation item in musicalnotations) { item.isselected = true; } } private void notifypropertychanged(string propertyname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } }

view

<window ...> <window.resources> </window.resources> <window.inputbindings> ... </window.inputbindings> <grid> ... <itemscontrol grid.column="0" grid.row="0" itemssource="{binding musicalnotations, mode=oneway}" horizontalalignment="center" verticalalignment="center"> <itemscontrol.itemspanel> <itemspaneltemplate> <stackpanel isitemshost="true" orientation="horizontal"/> </itemspaneltemplate> </itemscontrol.itemspanel> <itemscontrol.itemtemplate> <datatemplate> <c:musicalnotationbox/> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> </grid>

thanks.

update, musicalnotationbox xaml

<usercontrol x:class="numberedmusicscoresusercontrol.musicalnotationbox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:numberedmusicscoresusercontrol.musicalnotationboxproperties" mc:ignorable="d"> <usercontrol.resources> <local:dotconverter x:key="dotconverter"/> <local:noteconverter x:key="noteconverter"/> <local:accidentalconverter x:key="accidentalconverter"/> <local:isselectedconverter x:key="isselectedconverter"/> </usercontrol.resources> <grid x:name="grid" margin="10,5,10,5" horizontalalignment="center" verticalalignment="center" background="{binding path=musicalnotation.isselected, converter={staticresource isselectedconverter}, mode=oneway}"> <grid.columndefinitions> <columndefinition width="auto"/> <columndefinition width="auto"/> <columndefinition width="auto"/> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="auto"/> <rowdefinition height="auto"/> <rowdefinition height="auto"/> </grid.rowdefinitions> <textblock grid.column="0" grid.row="1" text="b" visibility="{binding path=musicalnotation.accidental, converter={staticresource accidentalconverter}, converterparameter=fl, mode=oneway}" fontsize="15" fontfamily="couriernew" horizontalalignment="center" verticalalignment="center"/> <path grid.column="1" grid.row="1" stroke="black" strokethickness="1" stretch="fill" visibility="{binding path=musicalnotation.accidental, converter={staticresource accidentalconverter}, converterparameter=sp, mode=oneway}" > <path.data> <linegeometry startpoint="1,0" endpoint="0,1"> <linegeometry.transform> <rotatetransform centerx="0" centery="0" angle="30"/> </linegeometry.transform> </linegeometry> </path.data> </path> <textblock grid.column="1" grid.row="1" text="{binding path=musicalnotation.note, converter={staticresource noteconverter}, mode=oneway}" fontsize="15" fontfamily="couriernew" horizontalalignment="center" verticalalignment="center" margin="2.5,0,2.5,0"/> <itemscontrol grid.column="1" grid.row="0" itemssource="{binding path=musicalnotation.octave, converter={staticresource dotconverter}, converterparameter=top, mode=oneway}"> <itemscontrol.itemspanel> <itemspaneltemplate> <stackpanel isitemshost="true"/> </itemspaneltemplate> </itemscontrol.itemspanel> <itemscontrol.itemtemplate> <datatemplate> <ellipse horizontalalignment="center" verticalalignment="top" margin="{binding margin}" fill="{binding fill}" width="{binding diameter}" height="{binding diameter}"/> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> <itemscontrol grid.column="1" grid.row="2" itemssource="{binding path=musicalnotation.octave, converter={staticresource dotconverter}, converterparameter=bot, mode=oneway}"> <itemscontrol.itemspanel> <itemspaneltemplate> <stackpanel isitemshost="true"/> </itemspaneltemplate> </itemscontrol.itemspanel> <itemscontrol.itemtemplate> <datatemplate> <ellipse horizontalalignment="center" verticalalignment="bottom" margin="{binding margin}" fill="{binding fill}" width="{binding diameter}" height="{binding diameter}"/> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> <itemscontrol grid.column="2" grid.row="1" itemssource="{binding path=musicalnotation.dot, converter={staticresource dotconverter}, converterparameter=right, mode=oneway}"> <itemscontrol.itemspanel> <itemspaneltemplate> <stackpanel orientation="horizontal" isitemshost="true"/> </itemspaneltemplate> </itemscontrol.itemspanel> <itemscontrol.itemtemplate> <datatemplate> <ellipse horizontalalignment="left" verticalalignment="center" margin="{binding margin}" fill="{binding fill}" width="{binding diameter}" height="{binding diameter}"/> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> </grid>

(i didn't include since, think, if "singular" 1 correct, usercontrol right well. might wrong though.)

the datacontext user command musicalnotation object, while binding instead of using

background="{binding path=musicalnotation.isselected, converter={staticresource ....

just utilize

background="{binding path=isselected, converter={staticresource ....

c# wpf mvvm observablecollection

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -