c# - How to Bind ItemsSource with ObservableCollection in WPF -
in wpf application - add new item observablecollection
via button click event handler
. want show added item adds observablecollection
via binding
itemscontrol
wrote code not working. can solve problem. here code is:
.xaml file
<dxlc:scrollbox verticalalignment="top"> <itemscontrol x:name="lstitemsclassm" itemssource="{binding path=topp, mode=twoway}"> <itemscontrol.itemtemplate> <datatemplate> <stackpanel orientation="vertical"> <button content="{binding name}" tag="{binding pkid}"/> </stackpanel> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> </dxlc:scrollbox>
.cs file
public observablecollection<classmm> topp { get; set; } int dv , startindex, lastindex; public mainwindow() { initializecomponent(); topp = new observablecollection<classmm>(); startindex=dv=1; topp.add(new classmm() { pkid=dv, name = "test 1" }); dv=2; topp.add(new classmm() { pkid = dv, name = "test 2" }); dv = 3; topp.add(new classmm() { pkid = dv, name = "test 3" }); dv = 4; topp.add(new classmm() { pkid = dv, name = "test 4" }); lastindex=dv = 5; topp.add(new classmm() { pkid = dv, name = "test 5" }); } private void button_click(object sender, routedeventargs e) { lastindex = dv = dv++; topp.add(new classmm() { pkid = dv, name = musavebutton.content.tostring() }); foreach (var jk in topp.tolist()) { messagebox.show(jk.name); } } public class classmm : inotifypropertychanged { public string _name; public int _pkid; public int pkid { { return _pkid; } set { if (value != _pkid) { _pkid = value; notifypropertychanged(); } } } public string name { { return _name; } set { if (value != _name) { _name = value; notifypropertychanged(); } } } public event propertychangedeventhandler propertychanged; protected void notifypropertychanged(string propertyname = "") { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } }
}
keep xaml original , modify cs follows :
public observablecollection<classmm> topp { get; set; } private int dv, startindex, lastindex; public mainwindow() { initializecomponent(); datacontext = this; topp = new observablecollection<classmm>(); startindex = dv = 1; topp.add(new classmm() {pkid = dv, name = "test 1"}); dv = 2; topp.add(new classmm() {pkid = dv, name = "test 2"}); dv = 3; topp.add(new classmm() {pkid = dv, name = "test 3"}); dv = 4; topp.add(new classmm() {pkid = dv, name = "test 4"}); lastindex = dv = 5; topp.add(new classmm() {pkid = dv, name = "test 5"}); } private void button_click(object sender, routedeventargs e) { lastindex = dv = dv++; topp.add(new classmm() { pkid = dv, name = musavebutton.content.tostring() }); foreach (var jk in topp.tolist()) { messagebox.show(jk.name); } } public class classmm : inotifypropertychanged { public string _name; public int _pkid; public int pkid { { return _pkid; } set { if (value != _pkid) { _pkid = value; notifypropertychanged("pkid"); } } } public string name { { return _name; } set { if (value != _name) { _name = value; notifypropertychanged("name"); } } } public event propertychangedeventhandler propertychanged; protected void notifypropertychanged(string propertyname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } }
Comments
Post a Comment