c# - MVVM WPF ListBox Entry not updating -
i started learn mvvm pattern , created simple application test few things.
i have simple view with:
- listbox holding observablecollection of items
- delete button
- new button
- textbox item description
- textbox item value
everything works except fact that, if i'm updating item description listbox entry isn't updating. read articles this, think has collectionchanged isn't called. tried possible solutions problem, none of them worked. maybe there wrong approach.
hopefully can me problem.
model/item.cs
internal class item : inotifypropertychanged { #region fields private string value; private string description; #endregion #region constructors public item() { } public item(string value, string description) { this.description = description; this.value = value; } #endregion public string value { { return value; } set { this.value = value; onpropertychanged("value"); } } public string description { { return description; } set { description = value; onpropertychanged("description"); } } #region overrides public override string tostring() { return description; } #endregion string override #region inotifypropertychanged members public event propertychangedeventhandler propertychanged; private void onpropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (handler != null) { handler(this, new propertychangedeventargs(propertyname)); } } #endregion }
viewmodel/mainviewmodel.cs
... private observablecollection<item> items; private item selecteditem; public observablecollection<item> items { { return items; } set { items = value; onpropertychanged("items"); } } public item selecteditem { { return selecteditem; } set { selecteditem = value; onpropertychanged("selecteditem"); } } ...
view/mainwindow.xaml
... <button content="new" command="{binding newcommand}" /> <button content="delete" command="{binding deletecommand}" /> <listbox x:name="lbxitems" itemssource="{binding items}" selecteditem="{binding selecteditem}" /> <textbox text="{binding selecteditem.description}" /> <textbox text="{binding selecteditem.value}" /> ...
with itemtemplate should work
<listbox grid.row="4" grid.column="0" grid.columnspan="2" itemssource="{binding items}" selecteditem="{binding selecteditem}" > <listbox.itemtemplate> <datatemplate> <stackpanel orientation="horizontal"> <textblock text="{binding path=value}" margin="0,0,10,0/> <textblock text="{binding path=description}" /> </stackpanel> </datatemplate> </listbox.itemtemplate> </listbox>