c# - Set DataContext in XAML Rather Than in Code Behind -



c# - Set DataContext in XAML Rather Than in Code Behind -

i've read in other posts in place of specifying datacontext in code behind can specified in xaml.

i have observablecollection declared , populated in constructor of main class set datacontext:

using system; using system.windows; using system.collections.objectmodel; namespace itemscontroldemo { public partial class mainwindow : window { public observablecollection<person> personlist { get; set; } public mainwindow() { initializecomponent(); personlist = new observablecollection<person>(); personlist.add(new person(16, "abraham", "lincoln")); personlist.add(new person(32, "franklin", "roosevelt")); personlist.add(new person(35, "john", "kennedy")); personlist.add(new person(2, "john", "adams")); personlist.add(new person(1, "george", "washington")); personlist.add(new person(7, "andrew", "jackson")); datacontext = this; } private void button_add_click(object sender, routedeventargs e) { personlist.add(new person(3, "thomas", "jefferson")); } private void button_remove_click(object sender, routedeventargs e) { personlist.remove(thedatagrid.selecteditem person); } } public class person { public person() { } public person(int id, string firstname, string lastname) { id = id; firstname = firstname; lastname = lastname; } public int id { get; set; } public string firstname { get; set; } public string lastname { get; set; } } }

if this, works in application.

however, if remove "datacontext = this;" constructor , instead set datacontext in window element of application

datacontext="{binding relativesource={relativesource self}}"

i no data.

this xaml set datacontext after have removed code behind:

<window x:class="itemscontroldemo.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:itemscontroldemo" title="items command demo" height="350" width="400" windowstartuplocation="centerscreen" datacontext="{binding relativesource={relativesource self}}"> <grid> <grid.rowdefinitions> <rowdefinition height="*"/> <rowdefinition height="auto"/> </grid.rowdefinitions> <datagrid grid.row="0" name="thedatagrid" selectedvaluepath="id" autogeneratecolumns="false" alternatingrowbackground="bisque" itemssource="{binding personlist}"> <datagrid.columns> <datagridtextcolumn header="id" binding="{binding path=id}"/> <datagridtextcolumn header="first name" binding="{binding path=firstname}"/> <datagridtextcolumn header="last name" binding="{binding path=lastname}"/> </datagrid.columns> </datagrid> <stackpanel grid.row="1" orientation="horizontal" horizontalalignment="left" verticalalignment="bottom"> <button content="add item" margin="5" click="button_add_click"/> <button content="remove item" margin="5" click="button_remove_click"/> </stackpanel> </grid> </window>

any help in doing incorrectly appreciated.

thanks!

you need setup source before calling initializecomponent

edit: need instantiate before initializecomponent, since it's observablecollection implements inotifycollectionchanged, if modify collection, grid updated changes.

public mainwindow() { personlist = new observablecollection<person>(); initializecomponent(); personlist.add(new person(16, "abraham", "lincoln")); personlist.add(new person(32, "franklin", "roosevelt")); personlist.add(new person(35, "john", "kennedy")); personlist.add(new person(2, "john", "adams")); personlist.add(new person(1, "george", "washington")); personlist.add(new person(7, "andrew", "jackson")); }

c# wpf xaml

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -