c# - When trying to delete an item from listbox through database -
error in system.invalidoperationexception: @ system.windows.media.visualtreehelper.getrelative(dependencyobject reference,relativekind relativekind) @ system.windows.media.visualtreehelper.getchildrencount(dependencyobject reference) at.......................................
this xaml code of listbox
<listbox height="617" x:name="lststudentname" selectionchanged="lststudentname_selectionchanged" canvas.top="89" width="480"> <listbox.itemtemplate> <datatemplate> <stackpanel orientation="horizontal" height="70" width="480"> <image source="/project;component/delete-128.png" width="60" name="deleteitem" tap="deleteitem_tap" height="45" /> <textblock text="{binding title}" width="400" height="60" x:name="txtblkextra" foreground="#ff395a95" fontsize="30" /> <textblock text="{binding linksweb}" width="400" height="60" x:name="linkedline" tag="{binding txtblkextra}" foreground="#ff395a95" fontsize="19" /> </stackpanel> </datatemplate> </listbox.itemtemplate> </listbox> this source delete item listbox
private void deleteitem_tap(object sender, system.windows.input.gestureeventargs e) { seek { var listboxitem = lststudentname.itemcontainergenerator.containerfromindex(lststudentname.selectedindex) listboxitem; var txtblk = findvisualchildbytype<textblock>(listboxitem, "txtblkextra"); = txtblk.text; using (linkdatacontext studentdb = new linkdatacontext(strconnectionstring)) { var remove = (from r in studentdb.gettable<linkinfo>() r.title == a.tostring() select r).firstordefault(); if (remove != null) { studentdb.gettable<linkinfo>().deleteonsubmit(remove); studentdb.submitchanges(); messagebox.show("delete data"); var c = b in studentdb.gettable<linkinfo>() select b.title; //lststudentname.itemssource = a; list<linkinfo> datasource = new list<linkinfo>(); foreach (var x in c) { datasource.add(new linkinfo() { title = x }); } this.lststudentname.itemssource = datasource; } } } grab (exception ex) { messagebox.show(ex.tostring()); } } and help of visualtreehelper
t findvisualchildbytype<t>(dependencyobject element, string name) t : class { if (element t && (element frameworkelement).name == name) homecoming element t; int childcount = visualtreehelper.getchildrencount(element); (int = 0; < childcount; i++) { t childelement = findvisualchildbytype<t> (visualtreehelper.getchild(element, i), name); if (childelement != null) homecoming childelement; } homecoming null; } it worked error seen.
error thrown at
int childcount = visualtreehelper.getchildrencount(element); reference not valid visual dependencyobject
hope 1 prepare helps
c#
No comments:
Post a Comment