c# - Two-way binding an ObservableCollection<string> to WPF DataGrid -
i've been tying different things / reading on issue awhile , have not yet found answer. guys can help.
i have observablecollection of type string. want bind collection datagrid , able edit/delete/add collection. here xaml:
<datagrid itemssource="{binding movies.titles}" canuserdeleterows="true" canuseraddrows="true" height="300"> <datagrid.columns> <datagridtextcolumn binding="{binding path=datacontext, relativesource={relativesource self}}"/> </datagrid.columns> </datagrid>
the same observablecollection bound listbox. want able edit collection using datagrid method (above) , see changes/edits in listbox. delete/add working correctly, when edit string inside grid cell , loses focus, string goes , never gets updated.
thanks lot / suggestions.
wow, went yesterday , stuck datagrid add new line observablecollection. after research, realized why. strings , immutable.
i found question , unfortunately didn't have answer. can't leave empty of answer.
so here answers found:
datagrid cannot update string collection adding, editing, or removing strings.
i found workaround wrap string in stringwrapper object. here is.
public class stringwrapper { public string text { get; set; } }
i didn't either answer.
the original question asker, moncadad, looks wants 1 column datagrid. wants add , remove strings observablecollection without lot of code. edit isn't important since can done delete , add again.
i ended doing myself reusable usercontrol made called stringlistbox.
a listbox strings supports add , delete
basically idea create of datagrid label, listbox, textbox , add button , since control, has work observableobject or list in 1 control.
this give add , delete. doesn't provide edit.
hope helps next guy.
Comments
Post a Comment