File ListView, change name direct in ListView, WPF C# MVVM -
i made little program have files of folder , type in 1 listview. change name of file directly inside of listview.
i made listview textboxes in 1 column , name of file written inside textbox. can change name in textbox now, not change file name. how connection between textbox in listview , method change name? yes little lost here. pretty fresh in mvvm wpf.
my xaml code of listview:
<listview name="lvfiles" grid.column="0" itemssource="{binding fileslist}" selectionmode="single" selecteditem="{binding selectedfiles}" datacontext="{binding }" height="140"> <listview.view> <gridview x:name="gridfiles"> <gridviewcolumn> <gridviewcolumn.celltemplate> <datatemplate> <checkbox tag="{binding id}" ischecked="{binding relativesource={relativesource ancestortype={x:type listviewitem}}, path=isselected}"/> </datatemplate> </gridviewcolumn.celltemplate> </gridviewcolumn> <gridviewcolumn x:name="filename" header="name"> <gridviewcolumn.celltemplate> <datatemplate> <textbox text="{binding filename, mode=twoway, updatesourcetrigger=propertychanged}" /> </datatemplate> </gridviewcolumn.celltemplate> </gridviewcolumn> <gridviewcolumn x:name="filecreated" header="created" displaymemberbinding="{binding filecreated}" width="auto"/> <gridviewcolumn x:name="filechanged" header="changed" displaymemberbinding="{binding filechanged}" width="auto"/> </gridview> </listview.view> </listview>
my viewmodel is:
public class files : viewmodelbase { private int fileid; private string filename; private string filecreated; private string filechanged; public string filename { { return filename;} set { filename = value; notifypropertychanged("filename"); } }
in view model:
// should store file extension somewhere private string location; private string fileextension; private string _filename; public string filename { { return this._filename; set { if (_filename != value) { try { var source = path.combine(location, _filename + "." + fileextension); var destination = path.combine(location, value + "." + fileextension); //if store file extension dot, should remove dot path.combine file.move(source, destination); _filename = value; // invokepropertychanged("filename"); if needed } catch (exception e) { messagebox.show(e.tostring(), "error", messageboxbutton.ok, messageboximage.error); } } } }
Comments
Post a Comment