c# - Datagrid itemsource twoway databinding not working -



c# - Datagrid itemsource twoway databinding not working -

i seek build wpf based application datagrid , itemsource bound object.

xaml

<datagrid itemssource="{binding folders, mode=twoway}" autogeneratecolumns="false" horizontalscrollbarvisibility="visible" verticalscrollbarvisibility="visible"> <datagrid.columns> <datagridtemplatecolumn header="path" canuserresize="false"> <datagridtemplatecolumn.celltemplate> <datatemplate> <grid> <grid.columndefinitions> <columndefinition width="260"></columndefinition> <columndefinition width="30"></columndefinition> <columndefinition width="30"></columndefinition> </grid.columndefinitions> <textbox grid.column="0" text="{binding path}"> <textbox.style> <style targettype="{x:type textbox}"> <style.triggers> <trigger property="ismouseover" value="true"> <setter property="cursor" value="arrow"></setter> </trigger> </style.triggers> </style> </textbox.style> </textbox> </grid> </datatemplate> </datagridtemplatecolumn.celltemplate> </datagridtemplatecolumn> </datagrid.columns> </datagrid>

and generated source code

public partial class foldercontrol : usercontrol { public folderviewmodel folderviewmodel = new folderviewmodel("sourcefolders.xml"); public foldercontrol() { initializecomponent(); datacontext = folderviewmodel; } private textbox gettextboxinstance(object obj) { var btn = (button) obj; var parent = (grid) btn.parent; var text = (textbox) parent.children.cast<uielement>().select(tb => tb).first(); homecoming text; } private void openfolder_onclick(object sender, routedeventargs e) { var text = gettextboxinstance(sender); win32.folderbrowserdialog fbd = new win32.folderbrowserdialog(); win32.dialogresult showdialog = fbd.showdialog(); text.text = fbd.selectedpath; } private void openfile_onclick(object sender, routedeventargs e) { var text = gettextboxinstance(sender); win32.openfiledialog ofd = new win32.openfiledialog(); if (ofd.showdialog() == system.windows.forms.dialogresult.ok) { // if find same file on collection, show msg error if (folderviewmodel.folders.any(file => file.path == ofd.filename)) { messageboxresult result = messagebox.show(en.text4, "info", messageboxbutton.ok); } else { text.text = ofd.filename; } } } }

and viewmodel(i not gonna show whole code here, necessary)

public class folderviewmodel : uploadviewmodelbase { public bindinglist<folder> folders { get; set; } public folderviewmodel(string filename) : base(filename) { }

here utilize bindinglist generics type collection instead observecollection, observe alter on collection. , lastly file, model

public class folder : observableobject { private string _path; private ostype _os; private string _stros; public string path { { homecoming _path; } set { _path = value; raisepropertychanged(() => this.path); } } public ostype os { { homecoming _os; } set { _os = value; raisepropertychanged(() => this.os); } } public string stros { { homecoming _stros; } set { _stros = value; raisepropertychanged(() => this.stros); } } }

the whole problem here, when alter content row, not alter on collection object. wrong?

you have had confusion info binding. here tips:

two-way bindings used on controls display and can edit info bound values, not container control's properties... means there no point on setting mode="two-way" on itemssource property, because if @ itemscontrol.itemssource property page on msdn, you'll see supports one-way bindings.

as mentioned, setting mode="two-way" should used on controls can display and can edit info bound values. if on textbox.text property page on msdn, should see section named dependency property information. in section, can see textbox.text property binds two-way default, never have apply setting it.

so, while solution utilize following:

<textbox grid.column="0" text="{binding path, mode=twoway, updatesourcetrigger=propertychanged}" ... />

i'm sure updatesourcetrigger property needed set, because know now, setting mode=twoway on textbox.text property nothing:

<textbox grid.column="0" text="{binding path, updatesourcetrigger=propertychanged}" ... />

c# wpf xaml datagrid

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -