c# - Databinding a Datagrid -
after being unsuccessful in databinding observable collection datagrid (another question in same forum), tried reduce scope. project has 1 datagrid, 1 observablecolection , 1 class. still databinding failing.. please help..
using system.collections.objectmodel; using system.windows; namespace testdatagrid { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { public mainwindow() { initializecomponent(); } } public class mainviewmodel { public observablecollection<optionstrike> oos = new observablecollection<optionstrike>(new optionstrike[] { new optionstrike("put", 7500.00, 12345), new optionstrike("call", 7500.00, 123), new optionstrike("put", 8000.00, 23645), new optionstrike("call", 8000.00,99999) }); } public class optionstrike { public optionstrike(string p1, double p2, int p3) { // todo: complete member initialization this.type = p1; this.strike = p2; this.volume = p3; } public string type { get; set; } public double strike { get; set; } public double volume { get; set; } } }
this xaml..
<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:testdatagrid" x:class="testdatagrid.mainwindow" title="mainwindow" height="350" width="525"> <window.datacontext> <local:mainviewmodel/> </window.datacontext> <grid> <stackpanel> <datagrid itemssource="{binding oos}" autogeneratecolumns="true" /> </stackpanel> </grid> </window>
you need expose observablecollection
property, not field.
public class mainviewmodel { public observablecollection<optionstrike> oos { get; set; } public mainviewmodel() { oos = new observablecollection<optionstrike>(new optionstrike[] { new optionstrike("put", 7500.00, 12345), new optionstrike("call", 7500.00, 123), new optionstrike("put", 8000.00, 23645), new optionstrike("call", 8000.00,99999) }); } }
you cannot bind fields, see here more information on subject.
Comments
Post a Comment