c# - How can I populate DataGrid control with strings? -
i trying show list of strings in datagrid, haven't been able make work correctly. xaml is:
<datagrid x:name="listgrid" itemssource = "{binding}" autogeneratecolumns="true"> <datagrid.columns> <datagridtextcolumn header="title" binding="{binding tracktitle}"></datagridtextcolumn> </datagrid.columns> </datagrid>
the welcome.xaml.cs looks this, tracktitle list
public welcome() { initializecomponent(); listgrid.datacontext = mainwindow.tracktitle; }
when preview it, can see correct number of rows no data title column. there column generated length , shows correct length each string. doing wrong? binding parameter title column {binding tracktitle} not right?
look @ example:
view
<window x:class="example.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <stackpanel> <datagrid itemssource="{binding entries}"/> </stackpanel> </window>
view code-behind
namespace example { using system.windows; public partial class mainwindow : window { public mainwindow() { datacontext = new mainwindowviewmodel(); initializecomponent(); } } }
viewmodel
namespace example { using system.collections.objectmodel; using system.windows.controls; class mainwindowviewmodel { public observablecollection<string> entries { get; set; } public mainwindowviewmodel() { list<string> list = new list<string>() { "entry 1", "entry 2", "entry 3" }; entries = new observablecollection<string>(list); } } }
shows example of how bind collection property in viewmodel. try applying situation.
good luck!
Comments
Post a Comment