c# - WPF How to make listbox items editable? -
i need add "edit selected item" functionality listbox. need either of two:
ctrl+click on list item , magically becomes editable textbox or , when edited update data in database.
press "edit" button under listbox, open new edit window, edit data there, update database , update listbox when edit window closed.
problems encountered:
i want have first variant of solution don't know how implement that.
implementing second variant didn't how should update listbox after editing selected item in new window.
here's xaml of listbox:
<grid> <listbox x:name="lstqueries" horizontalalignment="left" height="228" verticalalignment="top" width="482" fontfamily="helveticaneuecyr" fontsize="16" itemssource="{binding queries}" mousedoubleclick="lstqueries_mousedoubleclick"> <listbox.itemtemplate> <datatemplate> <stackpanel> <label content="{binding name}" fontweight="medium" fontsize="18" fontfamily="helvetica"/> <textblock text="{binding text}" fontsize="16" fontfamily="helvetica"/> </stackpanel> </datatemplate> </listbox.itemtemplate> </listbox> <button x:name="btnedit" content="edit" horizontalalignment="left" margin="0,228,0,0" verticalalignment="top" width="482" height="43" click="btnedit_click"/> </grid>
and here's edit window xaml like:
<grid> <textbox x:name="txtname" horizontalalignment="left" height="23" margin="99,13,0,0" textwrapping="wrap" verticalalignment="top" width="183" fontfamily="helveticaneuecyr" fontsize="16"/> <label content="name:" horizontalalignment="left" margin="10,10,0,0" verticalalignment="top" fontfamily="helveticaneuecyr" fontsize="16"/> <textbox x:name="txttext" horizontalalignment="left" height="116" margin="65,45,0,0" textwrapping="wrap" verticalalignment="top" width="217" fontfamily="helveticaneuecyr" fontsize="16"/> <label content="text:" horizontalalignment="left" margin="10,86,0,0" verticalalignment="top" fontfamily="helveticaneuecyr" fontsize="16" height="27" width="50"/> <button x:name="btnsave" content="save" horizontalalignment="left" margin="10,166,0,0" verticalalignment="top" width="132" height="33"/> <button x:name="btncancel" content="cancel" horizontalalignment="left" margin="150,166,0,0" verticalalignment="top" width="132" height="33" click="btncancel_click"/> </grid>
and here's how access selecteditem in listbox:
if (lstqueries.selectedindex < 0) return; dynamic item = lstqueries.selecteditem dynamic; string name = item.name; string text = item.text;
'queries' binded listbox observablecollection<query>
query
looks this:
public class query { public string id { get; set; } public string name { get; set; } public string text { get; set; } public string autoschool { get; set; } }
so, me please implementing either of solutions?
why use dynamic? can cast selecteditem
query
without problem. once have it, need load properties edit window , handle editing here.
your query needs implement inotifypropertychanged
. otherwise won't see changes in listbox
(because control not aware of them). https://msdn.microsoft.com/en-us/library/vstudio/ms229614(v=vs.100).aspx
also, easier use binding
. don't forget set binding mode twoway
properties updated once change them. http://blog.scottlogic.com/2012/04/20/everything-you-wanted-to-know-about-databinding-in-wpf-silverlight-and-wp7-part-two.html
for window. prepare there single query
property (named selectedquery example) set selected instance. can bind textboxes (in window) using this: text="{binding selectedquery.name, mode="twoway"}"
. if have corretly implemented inotifypropertychanged
changing text in window should change in listview
.
Comments
Post a Comment